在android中有2个表创建的sqlite数据库出错,遗憾的是,“dbtest2已停止”

时间:2015-06-27 06:36:33

标签: android sqlite multiple-tables

package com.example.dbtest2;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {
    public static String DATABASE_NAME="riyas.db";
    public static int DATABASE_VERSION=1;



    //table login
    public static String TABLE_NAME1="login";

    public static String COL_L1="ID";
    public static String COL_L2="USERNAME";
    public static String COL_L3="PASSWORD";

    //table record
    public static String TABLE_NAME2="record";

    public static String COL_R1="ID";
    public static String COL_R2="NAME";
    public static String COL_R3="ADDRESS";
    public static String COL_R4="MOBILE";
    public static String COL_R5="MALE";
    public static String COL_R6="FEMALE";
    public static String COL_R7="DOC_NAME";
    public static String COL_R8="CHECK1";
    public static String COL_R9="CHECK2";

    public static String mytab1="create table"+TABLE_NAME1+" ("
            +COL_L1 +" INTEGER PRIMARY KEY AUTOINCREMENT,NOT NULL,"
            +COL_L2 +"TEXT,NOT NULL,"
            +COL_L3+ "TEXT,NOT NULL,"
            +");";


     public static String mytab2="create table"+TABLE_NAME2+" ("
            + COL_R1+ "INTEGER PRIMARY KEY AUTOINCREMENT,NOT NULL,"
            + COL_R2 +"TEXT,"
             +COL_R3+ "TEXT,"
             +COL_R4 +"INTEGER,"
             +COL_R5 +"TEXT,"
             +COL_R6 +"TEXT,"
            + COL_R7 +"TEXT,"
            + COL_R8+"TEXT,"
            + COL_R9 +"TEXT,"
              +");";

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        // TODO Auto-generated constructor stub
        SQLiteDatabase Cdb=this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase Cdb) {
        // TODO Auto-generated method stub
        Cdb.execSQL(mytab1);
       Cdb.execSQL(mytab2);
               }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
    db.execSQL("DROP TABLE IF EXIST"+TABLE_NAME1);
    db.execSQL("DROP TABLE IF EXIST"+TABLE_NAME2);

    onCreate(db);
    }
}

我的主要活动是......

package com.example.dbtest2;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class MainActivity extends ActionBarActivity {
    DataBaseHelper mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mydb=new DataBaseHelper(this);

    }
}

logcat结果是....

06-27 02:09:36.392: E/AndroidRuntime(13937): FATAL EXCEPTION: main
06-27 02:09:36.392: E/AndroidRuntime(13937): Process:
com.example.dbtest2, PID: 13937 06-27 02:09:36.392:
E/AndroidRuntime(13937): java.lang.RuntimeException: Unable to start
activity
ComponentInfo{com.example.dbtest2/com.example.dbtest2.MainActivity}:
android.database.sqlite.SQLiteException: near "tablelogin": syntax
error (code 1): , while compiling: create tablelogin (ID INTEGER
PRIMARY KEY AUTOINCREMENT,NOT NULL,USERNAMETEXT,NOT
NULL,PASSWORDTEXT,NOT NULL,); 06-27 02:09:36.392:
E/AndroidRuntime(13937):    at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.app.ActivityThread.access$800(ActivityThread.java:135) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.os.Handler.dispatchMessage(Handler.java:102) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
android.os.Looper.loop(Looper.java:136) 06-27 02:09:36.392:
E/AndroidRuntime(13937):    at
android.app.ActivityThread.main(ActivityThread.java:5017) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
java.lang.reflect.Method.invokeNative(Native Method) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
java.lang.reflect.Method.invoke(Method.java:515) 06-27 02:09:36.392:
E/AndroidRuntime(13937):    at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
dalvik.system.NativeStart.main(Native Method) 06-27 02:09:36.392:
E/AndroidRuntime(13937): Caused by:
android.database.sqlite.SQLiteException: near "tablelogin": syntax
error (code 1): , while compiling: create tablelogin (ID INTEGER
PRIMARY KEY AUTOINCREMENT,NOT NULL,USERNAMETEXT,NOT
NULL,PASSWORDTEXT,NOT NULL,); 06-27 02:09:36.392:
E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method) 06-27 02:09:36.392: E/AndroidRuntime(13937):    at
 android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
com.example.dbtest2.DataBaseHelper.onCreate(DataBaseHelper.java:61)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
 android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
 android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
com.example.dbtest2.DataBaseHelper.<init>(DataBaseHelper.java:55)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
com.example.dbtest2.MainActivity.onCreate(MainActivity.java:21) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
android.app.Activity.performCreate(Activity.java:5231) 06-27
02:09:36.392: E/AndroidRuntime(13937):  at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-27 02:09:36.392: E/AndroidRuntime(13937):    at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-27 02:09:36.392: E/AndroidRuntime(13937):    ... 11 more 06-27
02:09:43.582: I/Process(13937): Sending signal. PID: 13937 SIG: 9

2 个答案:

答案 0 :(得分:1)

您的表格创建错误:
您添加了一些不需要的逗号,并且也没有提供一些必需的空格

这些是正确的

public static String mytab1 = "CREATE TABLE " + TABLE_NAME1 + " ("
    + COL_L1 + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
    + COL_L2 + " TEXT NOT NULL,"
    + COL_L3 + " TEXT NOT NULL)";

public static String mytab2 = "CREATE TABLE " + TABLE_NAME2 + " ("
    + COL_R1 + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
    + COL_R2 + " TEXT,"
    + COL_R3 + " TEXT,"
    + COL_R4 + " INTEGER,"
    + COL_R5 + " TEXT,"
    + COL_R6 + " TEXT,"
    + COL_R7 + " TEXT,"
    + COL_R8 + " TEXT,"
    + COL_R9 + " TEXT)";

答案 1 :(得分:-1)

初看起来,它似乎是你的表定义(在错误的地方有太多逗号),尝试改为:

PageTwoFragment

您的变量名和数据类型之间也没有空格。 LogCat告诉您有语法错误。