· 8 years ago · Aug 13, 2018, 05:42 PM
1Unable to create second table in sqlite
2//Table 1
3private static final String TAG = "DBAdapter";
4private static final String DATABASE_TABLE = "registrationTable";
5private static final String DATABASE_NAME = "project1database";
6private static final int DATABASE_VERSION = 1;
7private static final String DATABASE_CREATE =
8 "create table registrationTable ( phone integer primary key not null, name text not null, " +
9 "age integer not null, area text not null, sex text not null);";
10
11//Table 2
12private static final String PURPOSE_TABLE = "purposeTable";
13private static final String PURPOSE_CREATE = "create table purposeTable ( phone integer not null, foreign key (phone) references registrationTable(phone), " +
14 "cdate text not null, primary key (phone, date), text1 text not null, text2 text not null);";
15
16private final Context context;
17private SQLiteDatabase db;
18private DatabaseHelper DBHelper;
19
20public DBAdapter(Context ctx){
21 context = ctx;
22}
23//public DBAdapter read()throws SQLException
24//public DBAdapter write()throws SQLException
25//public void close()
26//public long insertDetails(String name, int age, String area, int phone, String sex)
27
28public long insertPurpose(String date, String text1, String text2){
29 ContentValues initialValues1 = new ContentValues();
30 initialValues1.put(CDATE, date);
31 initialValues1.put(TEXT1, text1);
32 initialValues1.put(TEXT2, text2);
33 return db.insert(PURPOSE_TABLE, null, initialValues1);
34}
35
36private static class DatabaseHelper extends SQLiteOpenHelper{
37
38 public DatabaseHelper(Context context) {
39 super(context, DATABASE_NAME, null, DATABASE_VERSION);
40 }
41
42 @Override
43 public void onCreate(SQLiteDatabase db) {
44 try{
45 db.execSQL(DATABASE_CREATE);
46 db.execSQL(PURPOSE_CREATE);
47 db.execSQL("PRAGMA foreign_keys = ON;");
48 }catch(SQLException e){
49 e.printStackTrace();
50 }
51 }
52
53 @Override
54 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
55 Log.w(TAG, "Upgrading from version " + oldVersion + " to " + newVersion + ". All data will be deleted.");
56 db.execSQL("DROP TABLE IF EXISTS registrationTable");
57 db.execSQL("DROP TABLE IF EXISTS purposeTable");
58 onCreate(db);
59 }
60}
61
62dbAdapter2.write();
63dbAdapter2.insertPurpose(cDate, text1, text2);
64dbAdapter2.close();
65
6611-17 01:29:17.023: I/SqliteDatabaseCpp(15095): sqlite returned: error code = 1, msg = no such table: purposeTable, db=/data/data/com.android.project1/databases/project1database
6711-17 01:29:17.143: E/SQLiteDatabase(15095): Error inserting text1=a text2=b cdate=17-11-2011
6811-17 01:29:17.143: E/SQLiteDatabase(15095): android.database.sqlite.SQLiteException: no such table: purposeTable: , while compiling: INSERT INTO purposeTable(text1,text2,cdate) VALUES (?,?,?)
6911-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
7011-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
7111-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
7211-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
7311-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
7411-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
7511-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1745)
7611-17 01:29:17.143: E/SQLiteDatabase(15095): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1618)
7711-17 01:29:17.143: E/SQLiteDatabase(15095): at com.android.project1.DBAdapter.insertPurpose(DBAdapter.java:113)
7811-17 01:29:17.143: E/SQLiteDatabase(15095): at com.android.project1.Purpose3Activity$1.onClick(Purpose3Activity.java:51)
7911-17 01:29:17.143: E/SQLiteDatabase(15095): at android.view.View.performClick(View.java:3460)
8011-17 01:29:17.143: E/SQLiteDatabase(15095): at android.view.View$PerformClick.run(View.java:13955)
8111-17 01:29:17.143: E/SQLiteDatabase(15095): at android.os.Handler.handleCallback(Handler.java:605)
8211-17 01:29:17.143: E/SQLiteDatabase(15095): at android.os.Handler.dispatchMessage(Handler.java:92)
8311-17 01:29:17.143: E/SQLiteDatabase(15095): at android.os.Looper.loop(Looper.java:137)
8411-17 01:29:17.143: E/SQLiteDatabase(15095): at android.app.ActivityThread.main(ActivityThread.java:4340)
8511-17 01:29:17.143: E/SQLiteDatabase(15095): at java.lang.reflect.Method.invokeNative(Native Method)
8611-17 01:29:17.143: E/SQLiteDatabase(15095): at java.lang.reflect.Method.invoke(Method.java:511)
8711-17 01:29:17.143: E/SQLiteDatabase(15095): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
8811-17 01:29:17.143: E/SQLiteDatabase(15095): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
8911-17 01:29:17.143: E/SQLiteDatabase(15095): at dalvik.system.NativeStart.main(Native Method)
90
9111-17 11:42:11.281: E/SQLiteDatabase(10172): Error inserting text1=e text2=d cdate=17-11-2011
9211-17 11:42:11.281: E/SQLiteDatabase(10172): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
9311-17 11:42:11.281: E/SQLiteDatabase(10172): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
9411-17 11:42:11.281: E/SQLiteDatabase(10172): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
9511-17 11:42:11.281: E/SQLiteDatabase(10172): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1745)
9611-17 11:42:11.281: E/SQLiteDatabase(10172): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1618)
9711-17 11:42:11.281: E/SQLiteDatabase(10172): at com.android.project1.DBAdapter.insertPurpose(DBAdapter.java:113)
9811-17 11:42:11.281: E/SQLiteDatabase(10172): at com.android.project1.Purpose3Activity$1.onClick(Purpose3Activity.java:51)
9911-17 11:42:11.281: E/SQLiteDatabase(10172): at android.view.View.performClick(View.java:3460)
10011-17 11:42:11.281: E/SQLiteDatabase(10172): at android.view.View$PerformClick.run(View.java:13955)
10111-17 11:42:11.281: E/SQLiteDatabase(10172): at android.os.Handler.handleCallback(Handler.java:605)
10211-17 11:42:11.281: E/SQLiteDatabase(10172): at android.os.Handler.dispatchMessage(Handler.java:92)
10311-17 11:42:11.281: E/SQLiteDatabase(10172): at android.os.Looper.loop(Looper.java:137)
10411-17 11:42:11.281: E/SQLiteDatabase(10172): at android.app.ActivityThread.main(ActivityThread.java:4340)
10511-17 11:42:11.281: E/SQLiteDatabase(10172): at java.lang.reflect.Method.invokeNative(Native Method)
10611-17 11:42:11.281: E/SQLiteDatabase(10172): at java.lang.reflect.Method.invoke(Method.java:511)
10711-17 11:42:11.281: E/SQLiteDatabase(10172): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10811-17 11:42:11.281: E/SQLiteDatabase(10172): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10911-17 11:42:11.281: E/SQLiteDatabase(10172): at dalvik.system.NativeStart.main(Native Method)
110
111create table purposeTable (
112 phone integer not null,
113 cdate text not null,
114 text1 text not null,
115 text2 text not null,
116 primary key (phone, cdate),
117 foreign key (phone) references registrationTable(phone)
118);
119
120create table purposeTable (
121phone integer not null,
122cdate text not null,
123text1 text not null,
124text2 text not null,
125
126primary key (phone, cdate),
127foreign key (phone) references registrationTable(phone));