I try to create multiple table in one database by using SQLiteOpenHelper
.
This is code for create table:
public void onCreate(SQLiteDatabase db) {
/**Create table for user account*/
String CREATE_TABLE_USER = String.format("CREATE TABLE %s " +
"(%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT)" ,
User.TABLE,
User.Column.ID,
User.Column.USERNAME,
User.Column.PASSWORD
);/**Create table for add subjects*/
String CREATE_SUBJECT_TABLE = String.format("CREATE TABLE "+Subjects.TABLE_S+" ("+
Subjects.Column.ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
Subjects.Column.USERNAME+" TEXT, "+Subjects.Column.SubName+" TEXT)");
db.execSQL(CREATE_SUBJECT_TABLE);
db.execSQL(CREATE_TABLE_USER);
db.execSQL("PRAGMA foreign_keys = ON;");
Log.i(TAG, CREATE_TABLE_USER);
Log.d("CREATE TABLE SUCCESSFUL",CREATE_SUBJECT_TABLE);
}
And this is code for add subject to table subject:
public long addSubject(Subjects subjects) {
mDatabase = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Subjects.Column.USERNAME,subjects.getUsername());
values.put(Subjects.Column.SubName,subjects.getSubname());
long result = mDatabase.insert(Subjects.TABLE_S, null, values);
mDatabase.close();
return result;
}
I create layout with field to input subject name and button to send subject name to database. This is code for that part:
mSubject = (EditText) findViewById(R.id.subjects);
mSelect = (Button) findViewById(R.id.button_sub);
mSelect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = mUser.getUsername();
String subjectName = mSubject.getText().toString().trim();
iSubjects.setUsername(username);
iSubjects.setSubname(subjectName);
//String subname =iSubjects.getSubname();
Subjects subject = new Subjects(username,subjectName);
long rowId = mManager.addSubject(subject);
if (rowId == -1) {
String message = iSubjects.getUsername()+""+iSubjects.getSubname();
//"Not have anything insert to table! : ERROR!!";
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
} else {
String message = getString(R.string.register_success);
Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
finish();
}
}
});
When I run the activity class.It show errors like this:
05-04 00:00:45.245 13336-13336/com.example.cash.app1 E/SQLiteLog﹕ (1) no such table: subject
05-04 00:00:45.253 13336-13336/com.example.cash.app1 E/SQLiteDatabase﹕ Error inserting username=c subjectName=asd
android.database.sqlite.SQLiteException: no such table: subject (code 1): , while compiling: INSERT INTO subject(username,subjectName) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at com.example.cash.app1.UserManager.addSubject(UserManager.java:131)
at com.example.cash.app1.SubjectActivity$1.onClick(SubjectActivity.java:58)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
I don't know what wrong with my code. I have tried to fix it by follow many website and many forums but it doesn't work. Any help is greatly appreciated. Thank you :)