Java 我能';t将数据插入SQLite和can';我没看见我的桌子
我无法将数据插入SQLite,也无法查看我的表。Java 我能';t将数据插入SQLite和can';我没看见我的桌子,java,android,database,sqlite,Java,Android,Database,Sqlite,我无法将数据插入SQLite,也无法查看我的表。 我试图在SQLite的DB Browser中查看该表,但我看不到插入的任何内容,也看不到我创建的行 此处插入DataBaseHelper中的方法: //Adding new trainee public void addTrainee(Trainee trainee) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new Cont
我试图在SQLite的DB Browser中查看该表,但我看不到插入的任何内容,也看不到我创建的行 此处插入DataBaseHelper中的方法:
//Adding new trainee
public void addTrainee(Trainee trainee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
int count = getShopsCount();
values.put(COL_ID, 1);
values.put(COL_USERNAME, trainee.getUsername());
values.put(COL_NAME, trainee.getName());
values.put(COL_PASS, trainee.getPassword());
values.put(COL_EMAIL, trainee.getEmail());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close();// Closing database connection
}
// Getting shops Count
public int getShopsCount() {
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
此处插入主要活动的注册:
public void confirminput(View v) {
//If one of the validate retuen false the validation faild.
if ( !validateEmail() || !validateUsername() || !validateName() || !validatePassword()) {
Toast.makeText(getApplicationContext(), " Validation NOT OK", Toast.LENGTH_LONG).show();
return;
}
//Inserting Trainee.
Trainee trainee = new Trainee();
trainee.setUsername(textInputUsername.getEditText().getText().toString().trim();
trainee.setName(textInputName.getEditText().getText().toString().trim());
trainee.setEmail(textInputEmail.getEditText().getText().toString().trim());
trainee.setPassword(textInputPassword.getEditText().getText().toString().trim();
//Insert Method data .
myDb.addTrainee(trainee);
}
onCreate():
谢谢你的帮助
问题是创建了错误的表
@Override
public void onCreate(SQLiteDatabase db) {
String createTraineeTable = "create table trainee ("+COL_ID+" integer primary key AUTOINCREMENT ," +
COL_USERNAME+" text not null, "+COL_NAME+" text not null, "+COL_PASS+" text not null,"+COL_EMAIL+" text not null );";
db.execSQL(createTraineeTable);
this.db = db;
}
你能告诉我这本书更容易被别人阅读吗?这太好了。请检查logcatno error中是否有任何错误。我真的不明白问题出在哪里。请尝试删除应用程序的数据或卸载应用程序,然后重新运行。然后它可能会起作用。数据库文件只有4k,这表示没有表(至少是您定义的表)。我猜您已经运行了该应用程序,但在
onCreate
方法(尝试创建表)中出现了问题。已更正问题,然后运行应用程序。当数据库存在时,onCreate
方法将不会运行。i、 e.onCreate
方法在数据库的生命周期内只运行一次(删除应用程序的数据将删除数据库)。您可能希望删除行values.put(COL_ID,1)代码>。这将允许您只添加1行,因为后续行将被跳过,因为COL_ID列必须是唯一的。不指定值(删除行)将允许SQLite分配唯一id(第一行可能是1,第二行可能是2,第三行可能是3……)。更多信息请点击这里。
@Override
public void onCreate(SQLiteDatabase db) {
String createTraineeTable = "create table trainee ("+COL_ID+" integer primary key AUTOINCREMENT ," +
COL_USERNAME+" text not null, "+COL_NAME+" text not null, "+COL_PASS+" text not null,"+COL_EMAIL+" text not null );";
db.execSQL(createTraineeTable);
this.db = db;
}