我的android studio应用程序出现严重问题!
我创建了一个带有两个表的数据库。插入数据时会发生问题。事实上,当我在表单中插入数据时,前一个数据将被删除!
------------例如-------------------
首次插入:
名称 - >一个
密码 - >
第二次插入:
名称 - > b
密码 - > b
但第一次插入被取消了!
我希望有人帮助我...我会用一些代码来理解我的问题:
DatabaseOperation.java
private static final String CREATETABLE_QUERY_USER =
"CREATE TABLE " + UserContact.DatabaseInfo.TABLE_USER + " ( "
+ UserContact.UserInfo.ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ UserContact.UserInfo.USERNAME + " TEXT NOT NULL, "
+ UserContact.UserInfo.PASSWORD + " TEXT NOT NULL, "
+ UserContact.UserInfo.EMAIL + " TEXT NOT NULL, "
+ UserContact.UserInfo.PHONE_NUMBER + " TEXT NOT NULL "
+ " );"
;
private static final String CREATETABLE_QUERY_PASSWORD =
"CREATE TABLE " + UserContact.DatabaseInfo.TABLE_PASS + " ( "
+ UserContact.PasswordTable.ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ UserContact.PasswordTable.NAME + " TEXT NOT NULL, "
+ UserContact.PasswordTable.POSTERPASSWORD + " INTEGER NOT NULL, "
+ UserContact.PasswordTable.DESCRIPTION + " TEXT NOT NULL, "
+ UserContact.PasswordTable.PASSWORD + " TEXT NOT NULL "
//+ " FOREIGN KEY(" + UserContact.PasswordTable.ID + ") REFERENCES " + UserContact.DatabaseInfo.TABLE_USER + "(" + UserContact.UserInfo.ID +")
+ " );"
;
public void addUser(SQLiteDatabase db, String username, String password, String email, String phone){
ContentValues contentValues = new ContentValues();
contentValues.clear();
contentValues.put(UserContact.UserInfo.USERNAME, username);
contentValues.put(UserContact.UserInfo.PASSWORD, password);
contentValues.put(UserContact.UserInfo.EMAIL, email);
contentValues.put(UserContact.UserInfo.PHONE_NUMBER, phone);
db.insert(UserContact.DatabaseInfo.TABLE_USER, null, contentValues);
}
public void addPassword(SQLiteDatabase db, String name, String note, String password, int posterpassword){
ContentValues contentValues = new ContentValues();
contentValues.clear();
if(password.length() > 0){
contentValues.put(UserContact.PasswordTable.POSTERPASSWORD, posterpassword);
contentValues.put(UserContact.PasswordTable.NAME, name);
contentValues.put(UserContact.PasswordTable.DESCRIPTION, note);
contentValues.put(UserContact.PasswordTable.PASSWORD, password);
db.insert(UserContact.DatabaseInfo.TABLE_PASS, null, contentValues);
Log.e("DATABASE OPERATION", " INSERT PASSWORD successfull in " + UserContact.DatabaseInfo.TABLE_PASS + "...");
}
}
NewContact.java
String name = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
String mail = emailEditText.getText().toString();
String phone = phoneEditText.getText().toString();
//Add user to database
String dbFile = getDatabasePath(UserContact.DatabaseInfo.DB_NAME).getPath();
databaseOperations = new DatabaseOperations(context);
sqLiteDatabase = SQLiteDatabase.openOrCreateDatabase(dbFile, UserContact.DatabaseInfo.password, null);
sqLiteDatabase = databaseOperations.getWritableDatabase(UserContact.DatabaseInfo.password);
databaseOperations.addUser(sqLiteDatabase, name, password, mail, phone);
Log.e("New Contact: " , "Success! -> username: " + name + ", password: " + password);
//End to add user----------------------------------
//After registration, return to authentication page
Intent intent_authenticationPage = new Intent(getApplicationContext(), AuthenticationPage.class);
startActivity(intent_authenticationPage);
答案 0 :(得分:0)
我没有获得您的数据库结构,但无论如何都尝试从
更改CREATE TABLE
语法
CREATE TABLE <table_name>(...)
到
CREATE TABLE IF NOT EXISTS <table_name>(...)