这个问题已经回答很多次但仍然无法找到我的错误。我正在创建一个数据库和一个表,以便在SQLite数据库中插入一些值。但它显示了SQLite Exception。
这是我尝试过的代码。
主要活动:
当我点击Register按钮时,它应该创建数据库,表格,然后将数据输入表格。
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "userDB";
private static final String TABLE_USER = "users";
private static final String KEY_ID = "id";
private static final String KEY_USERNAME = "username";
private static final String KEY_PASSWORD = "password";
db = new DataBaseHandler(RegisterActivity.this); // my database handler
username = (EditText) findViewById(R.id.usernameRegister);
password = (EditText) findViewById(R.id.passwordRegister);
registerSave = (Button) findViewById(R.id.saveRegister);
registerSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
usernameInput = username.getText().toString();
passwordInput = password.getText().toString();
//createDB();
Log.d("Insert: ", "Inserting ..");
db.addUsers(new Users(""+usernameInput,""+passwordInput));
// db.addUsers(new Users("password",passwordInput));
Log.d("inserted", ""+db);
Log.d("inputs", "" + usernameInput + ":" + passwordInput);
}
});
这是我的数据库处理程序类
public DataBaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS "+TABLE_USER+" (" + KEY_ID
+ "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USERNAME + " TEXT,"
+ KEY_PASSWORD + " TEXT" + ")";
db.execSQL(CREATE_USER_TABLE);
Log.d("sqlite DB:","Created"+db.toString());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+ TABLE_USER);
onCreate(db);
}
// Adding new contact
void addUsers(Users users) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USERNAME, users.getUsername()); // User Name
values.put(KEY_PASSWORD, users.getPassword()); // Password
// Inserting Row
db.insert(TABLE_USER, null, values);
db.close(); // Closing database connection
}
Users getUser(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_USER, new String[] { KEY_ID,
KEY_USERNAME, KEY_PASSWORD }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Users users = new Users(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return users;
}
这是获取用户数据的用户类。
public class Users {
int _id;
String _username;
String _password;
public Users() {
}
public Users(int id, String username, String password) {
this._id = id;
this._username = username;
this._password = password;
}
public Users(String username, String password) {
this._username = username;
this._password = password;
}
public int getId() {
return this._id;
}
public void setId(int id) {
this._id = id;
}
public String getUsername() {
return this._username;
}
public void setUsername(String username) {
this._username = username;
}
public String getPassword() {
return this._password;
}
public void setPassword(String password) {
this._password = password;
}
}
我也发布了输出的logcat:
Logcat:
01-07 07:38:19.613: E/SQLiteDatabase(3467): Error inserting username=sagar password=asdfg
01-07 07:38:19.613: E/SQLiteDatabase(3467): android.database.sqlite.SQLiteException: no such table: users (code 1): , while compiling: INSERT INTO users(username,password) VALUES (?,?)
01-07 07:38:19.613: E/SQLiteDatabase(3467): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
`
我很震惊。需要一些帮助。
答案 0 :(得分:2)
您WHITE SPACE
部分中的CREATE TABLE IF NOT EXISTS
受到了影响。
您可以使用这个
String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_USERNAME + " TEXT, " + KEY_PASSWORD + " TEXT )";
卸载&运行您的应用。快乐编码。
答案 1 :(得分:1)
尝试此查询
String CREATE_USERS_TABLE= java.lang.String.format("CREATE TABLE IF NOT EXISTS %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT);", TABLE_USER,KEY_ID, KEY_USERNAME,KEY_PASSWORD);
答案 2 :(得分:1)
首先,在创建表查询中搜索缺少的空格:
String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS "+TABLE_USER+" (" + KEY_ID
+ "INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_USERNAME + " TEXT,"
+ KEY_PASSWORD + " TEXT" + ")";
也许您在KEY_ID
和INTEGER PRIMARY KEY AUTOINCREMENT,...
之间找不到空格。您必须增加数据库版本才能再次调用public void onCreate(SQLiteDatabase db)
。
您可以记录您的数据库创建sql并在控制台中自己尝试。