[英]I am making a login and sign up page for android but the sqlite database i use crashes on onCreate
package company.mystorage.com.mystorage;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper{
private static final String DATABASE_NAME = "mystorage.db";
private static final String TABLE_NAME = "userlist";
public static final String COLUMN_1 ="Id";
private static final String COLUMN_2 = "username";
private static final String COLUMN_3 = "password";
SQLiteDatabase db;
public DatabaseHelper(Context context) {
// database is created when constructor is called.
super(context, DATABASE_NAME, null, 1);
// to invoke onCreate() and onUpgrade() methods to create the tables
SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("create table " + TABLE_NAME + "("
+ COLUMN_1 +" INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_2 +" TEXT, "
+ COLUMN_3 +" TEXT)"
);
}
public boolean insertData(String username, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_2, username);
contentValues.put(COLUMN_3, password);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
public boolean searchPass(String user){
db = this.getReadableDatabase();
String query = "select * from " + TABLE_NAME + " where " + COLUMN_2 + " = " + "'" + user + "'";
Cursor cursor = db.rawQuery(query, null);
cursor.moveToFirst();
if (cursor.getCount() > 0) {
return true;
}
cursor.close();
db.close();
return false;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
onCreate(db);
}
}
This code will create a database and also contains the functions to search and add users to the database. 此代码将创建一个数据库,并且还包含搜索用户并将用户添加到数据库的功能。 I tried to debug the code and it showed that it crashed when the onCreate was called. 我尝试调试代码,结果表明调用onCreate时崩溃。 I checked the onCreate but the query is being execute correctly. 我检查了onCreate,但查询正确执行。
Edited* I changed the this. 编辑*我改变了这个。 for the db but it still crashed. 为数据库,但它仍然崩溃。
public void onCreate(SQLiteDatabase db){ this.db.execSQL(...)
You should be calling execSQL()
on the db
parameter and not this.db
field that has not yet been initialized. 您应该在db
参数上调用execSQL()
,而不是尚未初始化的this.db
字段。
This should help : 这应该有帮助:
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
Please remove "this". 请删除“此”。
Also, in your method SearchPass(), if cursor has more count than it wont close the cursor and db object. 同样,在您的方法SearchPass()中,如果游标的计数大于其数量,则不会关闭游标和db对象。 It would be a better idea to handle that in Finally block. 最好在Final块中处理它。
在您的onCreate
方法中,请勿将this
与db.execSQL(..)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.