Android SQLite数据库没有这样的表格问题

时间:2016-09-01 15:19:15

标签: android database sqlite android-sqlite sqliteopenhelper

我在我的应用程序中使用SQLite数据库进行正常插入和获取目的。在尝试获取特定表的表行数时,我正面临没有这样的表问题

Logcat错误:

Fatal Exception: android.database.sqlite.SQLiteException: no such table: table_name(code 1): , while compiling: SELECT * FROM table_name
       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(SQLiteConnection.java)
       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.(SQLiteProgram.java:58)
       at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:37)
       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
       at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
       at com.takeoffandroid.database.DBAccess.getCount(SourceFile:384)
       at   com.takeoffandroid.database.DBAccess.isTableCountLogic(SourceFile:402)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:66)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.os.HandlerThread.run(HandlerThread.java:61)

SQLite创建表查询:

private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String INTEGER_PRIMARY_KEY = " INTEGER PRIMARY KEY AUTOINCREMENT";


public static final String TABLE_NAME = "table_name";

private static final String COLUMN_ID = "ID";

private static final String COLUMN_URL = "url";
private static final String COLUMN_JSON = "json";

private static final String CREATE_TABLE_QUERY = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
            COLUMN_ID + INTEGER_PRIMARY_KEY + COMMA_SEP +
            COLUMN_URL + TEXT_TYPE + COMMA_SEP +
            COLUMN_JSON + TEXT_TYPE + ")";



public static DBAccess init(Context context) {


        if (mInstance == null)
            synchronized (DBAccess.class) {
                if (mInstance == null) {
                    mInstance = new DBAccess(context);

                }
            }
        return mInstance;
    }

    public DBAccess(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_TABLE_QUERY);
    }

获取表格行数的方法:

 public  int getCount() {


        int count=0;
        SQLiteDatabase db=getReadableDatabase();
        Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, null);

        count = cursor.getCount();

        CLog.i("Table count", "Table count size is "+String.valueOf(count));

        cursor.close();
        db.close();
        close();

        return count;

    }

如果我的代码中存在任何问题,请帮助我,我很长时间以来一直在解决这个问题,而我无法找到解决方案。任何有点建议对我都有帮助。

注意:这是一个非常随机的问题,仅在少数设备上发生。

0 个答案:

没有答案