android - "exist"附近的 SQLite 故障

标签 android sql sqlite

我正在开发一个联系人列表应用程序,我需要创建 2 个表,因为我想进行连接。我试图在我的数据库中创建 2 个表,但在创建“创建 View ”时出现一些错误。

这是我得到的堆栈跟踪:

01-04 21:24:32.132: E/Database(4183):           Failure 1 (near "EXIST": syntax error) on 0x123c38 when preparing 'CREATE VIEW IF NOT EXIST ViewGroups AS SELECT contactTest1._id AS _id, contactTest1.name, contactTest1.phone, contactTest2.numegrup FROM contactTest1 JOIN contactTest2 ON contactTest1.idgrup =contactTest2._id'.
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   Couldn't open contacte.db for writing (will try read-only):
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   android.database.sqlite.SQLiteException: near "EXIST": syntax error: CREATE VIEW IF NOT EXIST ViewGroups AS SELECT contactTest1._id AS _id, contactTest1.name, contactTest1.phone, contactTest2.numegrup FROM contactTest1 JOIN contactTest2 ON contactTest1.idgrup =contactTest2._id
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1727)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at org.example.dbcontactconsole.DbCreate.onCreate(DbCreate.java:42)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:158)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at org.example.dbcontactconsole.DbContactConsole.getContacts(DbContactConsole.java:204)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at org.example.dbcontactconsole.DbContactConsole.showDatabaseContent(DbContactConsole.java:215)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at org.example.dbcontactconsole.DbContactConsole.onCreate(DbContactConsole.java:45)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.os.Handler.dispatchMessage(Handler.java:99)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.os.Looper.loop(Looper.java:123)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at android.app.ActivityThread.main(ActivityThread.java:4627)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at java.lang.reflect.Method.invokeNative(Native Method)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at java.lang.reflect.Method.invoke(Method.java:521)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-04 21:24:32.251: E/SQLiteOpenHelper(4183):   at dalvik.system.NativeStart.main(Native Method)

这是我创建的数据库的代码:

public class DbCreate extends SQLiteOpenHelper{
    private static final String DB_NAME = "contactsdataase.db";
    private static final int DB_VERSION = 2;

    /** Create a helper object for the Events database */
    public DbCreate(Context context) { 
      super(context, DB_NAME, null,DB_VERSION); 
    }

    @Override
    public void onCreate(SQLiteDatabase db) { 
        db.execSQL("CREATE TABLE " + DbConstants.TABLE_NUME + " " +
            "(" + DbConstants.Group_ID + " INTEGER PRIMARY KEY , " + 
                  DbConstants.GROUP_NAME+");");
        db.execSQL("CREATE TABLE " + DbConstants.TABLE_NAME + " " +
            "(" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
                  DbConstants.NAME + " TEXT NOT NULL," + 
                  DbConstants.PHONE + " TEXT NOT NULL," +
                  DbConstants.EMAIL + " TEXT NOT NULL," +
                  DbConstants.URL_STRING+ " TEXT NOT NULL,"+
                  DbConstants.ADRESS+ " TEXT NOT NULL,"+
                  DbConstants.Grupid+ "INTEGER NOT NULL ,FOREIGN KEY ("+DbConstants.Grupid+") REFERENCES "+DbConstants.TABLE_NUME+" ("+DbConstants.Group_ID+"));");

               db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
            " BEFORE INSERT "+
            " ON "+DbConstants.NAME+
            " FOR EACH ROW BEGIN"+
            " SELECT CASE WHEN ((SELECT "+DbConstants.Group_ID+" FROM "+DbConstants.TABLE_NUME+" WHERE "+DbConstants.Group_ID+"=new."+DbConstants.Grupid+" ) IS NULL)"+
            " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
        db.execSQL("CREATE VIEW IF NOT EXIST "+viewEmps+
        " AS SELECT "+employeeTable+"."+colID+" AS _id,"+
        " "+employeeTable+"."+colName+","+
        " "+employeeTable+"."+colAge+","+
        " "+deptTable+"."+colDeptName+""+
        " FROM "+employeeTable+" JOIN "+deptTable+
        " ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
        );          "  END;");     
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + DbConstants.TABLE_NUME);
        db.execSQL("DROP TABLE IF EXISTS " + DbConstants.TABLE_NAME);
        onCreate(db);
    }
}

最佳答案

它应该是 exists 而不是 exist (注意末尾的“s”)。

完整引用 View http://www.sqlite.org/lang_createview.html

关于android - "exist"附近的 SQLite 故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8732792/

相关文章:

mysql - 一张表上的 SQLite SELECT 语句

sql - 使 SSMS 启动并连接到数据库的 URL?

mysql - NOT LIKE 子句在 SQL 中不起作用

java - 从 AsyncTask 触发时 SQLiteOpenHelper.getWritableDatabase 上出现 NullPointerException

java - 是否有类似于 <android.support.v7.widget.CardView> 的 <CardView> 标签?

sql - 我如何在 SQL 中聚合它

ios - 备份和接收核心数据 SQlite3 数据库

android - 我如何模拟和测试这个类?

android - 使用 fragment 中的上下文时出错

android - SetMargin 在 RelativeLayout 中不起作用