2017-08-17 56 views
-2

我是android新手。我正在嘗試進行簡單的註冊和登錄系統。我已經使用SQLite數據庫系統。當應用程序打開時,應該創建數據庫和表,但它不會發生在我身上。數據文件夾內的「Android設備監視器」中未顯示數據庫。這裏是DatabaseHelper類的代碼。未在android上創建數據庫和表格

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)); 
     } 
    }); 

} 

謝謝

+1

您究竟在哪裏查找數據庫?請注意,數據庫所在的應用程序私有文件夾實際上是私人的,除非設備是根源的(例如模擬器),否則您無法在其中看到它。 – laalto

回答

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);"; 

希望它可以幫助:-)

+0

這並不重要。分號對於'execSQL()'是可選的。 (已經有一個關於它不需要的評論,但它被刪除了。) – laalto

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(可能不是,按照,AUTOINCR EMENT關鍵字會增加額外的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 
0

試試這個。

DatabaseHelper dbHelper = new DatabaseHelper(this); 
SQLiteDatabase database = dbHelper.getWritableDatabase(); 
database.insert(.....) 
database.update(.....) 
database.execSQL(...) 
database.rawQuery("qry", null);