Stackoverflow社区的一个大问题,
我目前正在使用Android Studio中的登录表单并尝试连接到外部Sqlite数据库,但是我遇到的问题是复制的数据库中似乎没有表。对于表User上的select,导致以下错误:
*E/SQLiteLog: (1) no such table: User*
即使我的数据库已被复制并被打开:
Database is open
有人可以告诉我我做错了什么吗?我在Databasehelper类中添加了我正在使用的代码:
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 = 'jack@androidtutorialshub.com' 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;
}
}
答案 0 :(得分:0)
我决定使用json来保存我的数据,用户将通过wifi访问。