android - Sqlite 返回 : error code = 1, msg = 没有这样的表:client_table

标签 android sqlite

我正在尝试编写一个包含两个 Activity 的应用程序。每个都使用单独的表格并获取表格中的内容并将其显示在 ListView 中。

到目前为止,第一个 Activity 可以访问 person_table 并显示该表中的所有内容,但是当我尝试启动客户端 Activity 时出现错误:

  sqlite returned: error code = 1, msg = no such table: client_table.

我需要帮助
package com.rich.myfinal;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

android.util.Log;

public class PersonDataHelper extends Activity {

    private static final String TAG = PersonDataHelper.class.getSimpleName();

    // database configuration
    // if you want the onUpgrade to run then change the database_version
    private static final String DATABASE_NAME = "mydatabase.db";

    // table configuration
    private static final String TABLE_NAME = "person_table";         // Table name
    private static final String TABLE_NAME1 = "client_table";         // Table name
    private static final String PERSON_TABLE_COLUMN_ID = "_id";     // a column named "_id" is required for cursor
    private static final String PERSON_TABLE_COLUMN_NAME = "person_name";
    private static final String PERSON_TABLE_COLUMN_PIN = "person_pin";

    private DatabaseOpenHelper openHelper;
    private SQLiteDatabase database;

    // this is a wrapper class. that means, from outside world, anyone will communicate with PersonDatabaseHelper,
    // but under the hood actually DatabaseOpenHelper class will perform database CRUD operations
    public PersonDataHelper(Context aContext) {

        openHelper = new DatabaseOpenHelper(aContext);
        database = openHelper.getWritableDatabase();
    }

    public void insertData (String aPersonName, String aPersonPin) {

        // we are using ContentValues to avoid sql format errors

        ContentValues contentValues = new ContentValues();

        contentValues.put(PERSON_TABLE_COLUMN_NAME, aPersonName);
        contentValues.put(PERSON_TABLE_COLUMN_PIN, aPersonPin);

        database.insert(TABLE_NAME, null, contentValues);
        database.insert(TABLE_NAME1, null, contentValues);
    }

    public Cursor getAllData () {

        String buildSQL = "SELECT * FROM " + TABLE_NAME;

        Log.d(TAG, "getAllData SQL: " + buildSQL);

        return database.rawQuery(buildSQL, null);
    }

    public Cursor getAllDataClient () {

        String buildSQL = "SELECT * FROM " + TABLE_NAME1;

        Log.d(TAG, "getAllDatayyy SQL: " + buildSQL);

        return database.rawQuery(buildSQL, null);
    }

    // this DatabaseOpenHelper class will actually be used to perform database related operation

    private class DatabaseOpenHelper extends SQLiteOpenHelper {

        public DatabaseOpenHelper(Context aContext) {
            super(aContext, DATABASE_NAME, null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            // Create your tables here

            String buildSQL = "CREATE TABLE " + TABLE_NAME + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_PIN + " TEXT )";

            String buildSQL2 = "CREATE TABLE IF NOT EXISTS" + TABLE_NAME1 + "( " + PERSON_TABLE_COLUMN_ID + " INTEGER PRIMARY KEY, " +
                    PERSON_TABLE_COLUMN_NAME + " TEXT, " + PERSON_TABLE_COLUMN_PIN + " TEXT )";

            Log.d(TAG, "onCreate SQL: " + buildSQL+buildSQL2);

            sqLiteDatabase.execSQL(buildSQL);
            sqLiteDatabase.execSQL(buildSQL2);
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
            // Database schema upgrade code goes here

            String buildSQL = "DROP TABLE IF EXISTS " + TABLE_NAME;
            String buildSQL2 = "DROP TABLE IF EXISTS " + TABLE_NAME1;

            Log.d(TAG, "onUpgrade SQL: " + buildSQL+buildSQL2);

            sqLiteDatabase.execSQL(buildSQL);       // drop previous table
            sqLiteDatabase.execSQL(buildSQL2);

            onCreate(sqLiteDatabase);               // create the table from the beginning
        }
    }
}

当我在客户端 Activity 中调用方法时出现错误
new Handler().post(new Runnable() {
            @Override
            public void run() {
                customAdapter = new CustomCursorAdapter(ClientActivity.this, databaseHelper.getAllDataClient());
                listView.setAdapter(customAdapter);
            }
        });
    }

最佳答案

我认为您在创建句子时出错(正如 Gunaseelan 所说),因此未创建 client_table。修改创建字符串后,您需要调用 DatabaseOpenHelper onCreate 或 onUpgrade 函数。

OnCreate 仅称为 如果数据库不存在 .如果删除数据库,下次打开它时会重新创建。

OnUpgrade 在数据库版本更改时调用。要调用它,请使用:

    public DatabaseOpenHelper(Context aContext) {
        super(aContext, DATABASE_NAME, null, 2);
    }

如您所见,我更改了数据库版本。这应该会触发 onUpgrade 函数以再次创建表。

无论您使用什么选项,请查看日志以检查创建表时是否有任何错误。

关于android - Sqlite 返回 : error code = 1, msg = 没有这样的表:client_table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17043164/

相关文章:

android - 为什么使用 Android NDK 通过 JNI 将 native 数组复制回 Java 无法正确填充?

android - 应用程序的 PagerAdapter 更改了适配器的内容

c# - SQLiteDataReader,最后一次Read()需10秒

php - 将远程数据库 (mysql) 与 sqlite 数据库同步,反之亦然

c++ - 应用程序连接到数据库

Android 资源$NotFoundException : Resource ID #0x0 Issue

android - 在 mac (NDK) 中构建 Telegram 源时出错

android - 如何获取android中选中的单选按钮的id?

sqlite - iOS App中的SQLite DB和表ID

Android SQlite 触发器