我是android新手。我正在尝试制作简单的注册和登录系统。我使用过SQLite数据库系统。当应用程序打开时,应该创建数据库和表,但它不会发生在我身上。数据文件夹内的“Android设备监视器”中没有显示数据库。这是DatabaseHelper类的代码。
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "dairy.db";
public static final String TABLE_NAME = "user_account";
public static final String COL_1 = "id";
public static final String COL_2 = "full_name";
public static final String COL_3 = "address";
public static final String COL_4 = "contact";
public static final String COL_5 = "password";
SQLiteDatabase db;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String tableUserAccount = "CREATE TABLE "+TABLE_NAME+"("
+COL_1+" INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, "
+COL_2+" TEXT NOT NULL, "
+COL_3+" TEXT NOT NULL, "
+COL_4+" TEXT NOT NULL, "
+COL_5+" TEXT NOT NULL)";
db.execSQL(tableUserAccount);
}
我的Main_Activty类
public class MainActivity extends AppCompatActivity {
private Button btn, btn1;
DatabaseHelper database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
database = new DatabaseHelper(this);
btn = (Button) findViewById(R.id.button);
btn1 = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, Login.class));
}
});
}
谢谢
答案 0 :(得分:2)
AUTOINCREMENT
只能用作列类型标识的一部分,并且必须紧跟 INTEGER PRIMARY KEY
。
你会遇到以下错误: -
08-18 14:23:18.865 12008-12008/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: mjt.so45722118, PID: 12008
java.lang.RuntimeException: Unable to start activity ComponentInfo{mjt.so45722118/mjt.so45722118.MainActivity}: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE user_account(id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, full_name TEXT NOT NULL, address TEXT NOT NULL, contact TEXT NOT NULL, password TEXT NOT NULL)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.database.sqlite.SQLiteException: near "AUTOINCREMENT": syntax error (code 1): , while compiling: CREATE TABLE user_account(id INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, full_name TEXT NOT NULL, address TEXT NOT NULL, contact TEXT NOT NULL, password TEXT NOT NULL)
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.executeSql(SQLiteDatabase.java:1674)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
at mjt.so45722118.DatabaseHelper.onCreate(DatabaseHelper.java:38)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at mjt.so45722118.DatabaseHelper.<init>(DatabaseHelper.java:24)
at mjt.so45722118.MainActivity.onCreate(MainActivity.java:47)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
因此你需要改变: -
+COL_1+" INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, "
到
+COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT, "
你甚至不能代码AUTOINCREMENT
(根据 可能不会.AUTOINCREMENT关键字会产生额外的CPU,内存,磁盘空间和磁盘I / O开销,如果没有则应该避免严格需要。通常不需要。 SQLite Autoincrement)。
INTEGER PRIMARY KEY
隐含NOT NULL
,因此不需要。
所以我建议只编码: -
+COL_1+" INTEGER PRIMARY KEY, "
您可以在MainActivity
之后将以下行添加到database = new DatabaseHelper(this);
: -
Cursor csr = database.getWritableDatabase().rawQuery("PRAGMA TABLE_INFO(" + DatabaseHelper.TABLE_NAME + ")",null)
String rowinfo;
while (csr.moveToNext()) {
rowinfo = "Row position=" + Integer.toString(csr.getPosition());
for (int i=0;i < csr.getColumnCount();i++) {
rowinfo = rowinfo + " Column=" + csr.getColumnName(i) +
" Value=" + csr.getString(i);
}
Log.d("TABLEINFO",rowinfo);
}
csr.close();
然后,这将在日志中显示以下内容,以向您显示数据库和表存在: -
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=0 Column=cid Value=0 Column=name Value=id Column=type Value=INTEGER Column=notnull Value=0 Column=dflt_value Value=null Column=pk Value=1
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=1 Column=cid Value=1 Column=name Value=full_name Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=2 Column=cid Value=2 Column=name Value=address Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=3 Column=cid Value=3 Column=name Value=contact Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
08-18 14:09:27.053 7905-7905/? D/TABLEINFO: Row position=4 Column=cid Value=4 Column=name Value=password Column=type Value=TEXT Column=notnull Value=1 Column=dflt_value Value=null Column=pk Value=0
答案 1 :(得分:0)
您错过了创建查询中的分号。试试这个
String tableUserAccount = "CREATE TABLE "+TABLE_NAME+"("
+COL_1+" INTEGER PRIMARY KEY NOT NULL AUTOINCREMENT, "
+COL_2+" TEXT NOT NULL, "
+COL_3+" TEXT NOT NULL, "
+COL_4+" TEXT NOT NULL, "
+COL_5+" TEXT NOT NULL);";
希望它有所帮助: - )