Ein großes Hallo an die Stackoverflow Gemeinschaft,26.02.2017 - Externe Sqlite 'Benutzer' Tabelle nicht gefunden, aber DB wurde kopiert. [Android]
ich zur Zeit auf einem Login-Formular arbeitete in Android Studio und zu versuchen, zu einer externen SQLite DB zu verbinden, aber ich habe ein Problem bin die Begegnung, wo es zu sein scheint keine Tabelle in der kopierten DB. Für eine ausgewählte auf dem Tisch Benutzer, führen in der folgenden erro:
*E/SQLiteLog: (1) no such table: User*
Auch wenn meine Datenbank kopiert wurde und geöffnet wird:
Database is open
Kann mir bitte jemand sagen, was ich falsch mache? Ich habe den Code hinzugefügt verwende ich in meiner Databasehelper Klasse:
public class DatabaseHelper extends SQLiteOpenHelper {
private Context mycontext;
Connection sqliteConnection;
// Database Version
private static final int DATABASE_VERSION = 1;
//Database Path
private static String DB_PATH ="/data/data/"+ BuildConfig.APPLICATION_ID+"/databases/";
// Database Name
private static final String DATABASE_NAME = "mystockdb.db";
// User table name
private static final String TABLE_USER = "User";
// User Table Columns names
private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_NAME = "name";
private static final String COLUMN_USER_PASSWORD = "user_password";
public SQLiteDatabase myDataBase;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.mycontext=context;
boolean dbexist = checkdatabase();
if (dbexist) {
System.out.println("Database exists");
opendatabase();
} else {
System.out.println("Database doesn't exist");
createdatabase();
}
}
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0) {
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
private boolean checkdatabase() {
boolean checkdb = false;
try {
String myPath = DB_PATH + DATABASE_NAME;
File dbfile = new File(myPath);
checkdb = dbfile.exists();
} catch(SQLiteException e) {
System.out.println("Database doesn't exist");
}
return checkdb;
}
public void opendatabase() throws SQLException {
//Open the database
String mypath = DB_PATH + DATABASE_NAME;
System.out.println(DB_PATH);
myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
if (myDataBase.isOpen()) {
System.out.println("Database is open");
}
}
public void createdatabase() {
boolean dbexist = checkdatabase();
if(dbexist) {
System.out.println(" Database exists.");
} else {
this.getReadableDatabase();
try {
copydatabase();
} catch(IOException e) {
throw new Error("Error copying database");
}
}
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion >oldVersion){
try{
copydatabase();
}catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* This method will return if user is in db or not
*
* @param name
* @param password
* @return true/false
*/
public boolean checkUser(String name, String password) {
// array of columns to fetch
String[] columns = {
COLUMN_USER_ID
};
opendatabase();
myDataBase = getReadableDatabase();
// selection criteria
String selection = COLUMN_USER_NAME + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ?";
// selection arguments
String[] selectionArgs = {name, password};
// query user table with conditions
/**
* Here query function is used to fetch records from user table this function works like we use sql query.
* SQL query equivalent to this query function is
* SELECT user_id FROM user WHERE user_email = '[email protected]' AND user_password = 'qwerty';
*/
Cursor cursor = myDataBase.query(TABLE_USER, //Table to query
columns, //columns to return
selection, //columns for the WHERE clause
selectionArgs, //The values for the WHERE clause
null, //group the rows
null, //filter by row groups
null); //The sort order
int cursorCount = cursor.getCount();
cursor.close();
myDataBase.close();
if (cursorCount > 0) {
return true;
}
return false;
}
}
Verwenden Sie stattdessen [SQLiteAssetHelper] (http://jgilfelt.github.io/android-sqlite-asset-helper/). –