引起的:android.database.sqlite。SQLiteException:没有这样的表:字符串:在编译时:选择id,字符串中id =?

时间:2023-01-17 23:01:32

I get this Error

我得到这个错误


07-16 20:58:27.299: E/AndroidRuntime(14005): Caused by: android.database.sqlite.SQLiteException: no such table: strings: , while compiling: SELECT id, string FROM strings WHERE  id = ?

I hava StringDB class that has ID and String to store objects of this class into SQLite Database. Please Debug it and give a solution. Here is the code:

我有一个StringDB类,它有ID和字符串将这个类的对象存储到SQLite数据库中。请调试它并给出一个解决方案。这是代码:

public class MySQLiteHelper extends SQLiteOpenHelper {


// Database Version
private static final int DATABASE_VERSION = 2;
// Database Name
private static final String DATABASE_NAME = "StringDB";

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

@Override
public void onCreate(SQLiteDatabase db) {
    // SQL statement to create StringDB table
    String CREATE_StringDB_TABLE = "CREATE TABLE StringDBs ( " +
            "id INTEGER PRIMARY KEY AUTOINCREMENT, " +"string"+ "TEXT )";

    // create StringDBs table
    db.execSQL(CREATE_StringDB_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older StringDBs table if existed
    db.execSQL("DROP TABLE IF EXISTS StringDBs");

    // create fresh StringDBs table
    this.onCreate(db);
}
//---------------------------------------------------------------------

/**
 * CRUD operations (create "add", read "get", update, delete) StringDB + get all StringDBs + delete all StringDBs
 */

// StringDBs table name
private static final String TABLE_STRINGS = "strings";

// StringDBs Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_STRING= "string";

private static final String[] COLUMNS = {KEY_ID,KEY_STRING};

public void addStringDB(StringDB string){

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put(KEY_STRING, string.getString()); // get author

    // 3. insert
    db.insert(TABLE_STRINGS, // table
            null, //nullColumnHack
            values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close();
}

public StringDB getStringDB(int id){

    // 1. get reference to readable DB
    SQLiteDatabase db = this.getReadableDatabase();

    // 2. build query
    Cursor cursor =
            db.query(TABLE_STRINGS, // a. table
            COLUMNS, // b. column names
            " id = ?", // c. selections
            new String[] { String.valueOf(id) }, // d. selections args
            null, // e. group by
            null, // f. having
            null, // g. order by
            null); // h. limit

    // 3. if we got results get the first one
    if (cursor != null)
        cursor.moveToFirst();

    // 4. build StringDB object
    StringDB res=new StringDB();
    res.setId(Integer.parseInt(cursor.getString(0)));
    res.setString(cursor.getString(1));


    return res;
}

// Get All StringDBs
public List<StringDB> getAllStringDBs() {
    List<StringDB> StringDBs = new LinkedList<StringDB>();

    // 1. build the query
    String query = "SELECT  * FROM " + TABLE_STRINGS;

    // 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);

    // 3. go over each row, build StringDB and add it to list
    StringDB StringDB = null;
    if (cursor.moveToFirst()) {
        do {
            StringDB = new StringDB();
            StringDB.setId(Integer.parseInt(cursor.getString(0)));
            StringDB.setString(cursor.getString(1));


            // Add StringDB to StringDBs
            StringDBs.add(StringDB);
        } while (cursor.moveToNext());
    }

    Log.d("getAllStringDBs()", StringDBs.toString());

    // return StringDBs
    return StringDBs;
}

 // Updating single StringDB
public int updateStringDB(StringDB StringDB) {

    // 1. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();

    // 2. create ContentValues to add key "column"/value
    ContentValues values = new ContentValues();
    values.put("strings", StringDB.getString()); // get author

    // 3. updating row
    int i = db.update(TABLE_STRINGS, //table
            values, // column/value
            KEY_ID+" = ?", // selections
            new String[] { String.valueOf(StringDB.getId()) }); //selection args

    // 4. close
    db.close();

    return i;

}

    // Deleting single StringDB
    public void deleteStringDB(StringDB StringDB) {

        // 1. get reference to writable DB
        SQLiteDatabase db = this.getWritableDatabase();

        // 2. delete
        db.delete(TABLE_STRINGS,
                KEY_ID+" = ?",
                new String[] { String.valueOf(StringDB.getId()) });

        // 3. close
        db.close();

        Log.d("deleteStringDB", StringDB.toString());

    }
}

4 个解决方案

#1


1  

  1. Under private static final String DATABASE_NAME = "StringDB"; add

    在私有静态最终字符串DATABASE_NAME = "StringDB";添加

    private static final String TABLE_NAME = "strings";
    
  2. Change String CREATE_StringDB_TABLE = "CREATE TABLE StringDBs ( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " +"string"+ "TEXT )";

    更改String CREATE_StringDB_TABLE = "CREATE TABLE StringDBs ("+ "id整数主键自动递增"+ " String "+ "TEXT ");

    to

    String CREATE_StringDB_TABLE = "CREATE TABLE " + TABLE_NAME + " (id " +
        "INTEGER PRIMARY KEY AUTOINCREMENT, string TEXT)";
    
  3. Change under onUpgrade @ db.execSQL

    修改onUpgrade @ db.execSQL。

    to

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    
  4. Change all references of TABLE_STRINGS to TABLE_NAME

    将TABLE_STRINGS的所有引用更改为TABLE_NAME。

#2


1  

You are passing the wrong table name. You are creating StringDB and asking for strings

您传递的是错误的表名。您正在创建StringDB并请求字符串。

#3


0  

You call the table StringDBs when you create it and strings later on. Choose one name.

当您稍后创建它和字符串时,您调用这个表StringDBs。选择一个名字。

#4


0  

Uninstall your app and install againa . this is due to that the latest table is not added to your to older one. the problem is because some of device is upgrading your app, so the checkDataBase() returning true , so you are not calling copyDataBase(). So you are using previous database which dont have generalSettings table . To solve this try-

卸载你的应用程序并重新安装。这是由于最新的表没有添加到您的旧表。问题在于一些设备正在升级你的应用程序,所以checkDataBase()返回true,所以你不会调用copyDataBase()。因此,您使用的是以前的数据库,而不是generalSettings表。尝试解决这个问题

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
  copyDatabase();
}

and

public InstallDB(Context context, String name) {
    super(context, name, null, DB_VERSION); 
    // DB_VERSION is an int,update it every new build

    this.ctx = context;
    this.DBNAME = name;
    this.DBPATH = this.ctx.getDatabasePath(DBNAME).getAbsolutePath();
    Log.e("Path 1", DBPATH);

}

Let me know if it works and if not ??

如果有用的话,请告诉我。

#1


1  

  1. Under private static final String DATABASE_NAME = "StringDB"; add

    在私有静态最终字符串DATABASE_NAME = "StringDB";添加

    private static final String TABLE_NAME = "strings";
    
  2. Change String CREATE_StringDB_TABLE = "CREATE TABLE StringDBs ( " + "id INTEGER PRIMARY KEY AUTOINCREMENT, " +"string"+ "TEXT )";

    更改String CREATE_StringDB_TABLE = "CREATE TABLE StringDBs ("+ "id整数主键自动递增"+ " String "+ "TEXT ");

    to

    String CREATE_StringDB_TABLE = "CREATE TABLE " + TABLE_NAME + " (id " +
        "INTEGER PRIMARY KEY AUTOINCREMENT, string TEXT)";
    
  3. Change under onUpgrade @ db.execSQL

    修改onUpgrade @ db.execSQL。

    to

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    
  4. Change all references of TABLE_STRINGS to TABLE_NAME

    将TABLE_STRINGS的所有引用更改为TABLE_NAME。

#2


1  

You are passing the wrong table name. You are creating StringDB and asking for strings

您传递的是错误的表名。您正在创建StringDB并请求字符串。

#3


0  

You call the table StringDBs when you create it and strings later on. Choose one name.

当您稍后创建它和字符串时,您调用这个表StringDBs。选择一个名字。

#4


0  

Uninstall your app and install againa . this is due to that the latest table is not added to your to older one. the problem is because some of device is upgrading your app, so the checkDataBase() returning true , so you are not calling copyDataBase(). So you are using previous database which dont have generalSettings table . To solve this try-

卸载你的应用程序并重新安装。这是由于最新的表没有添加到您的旧表。问题在于一些设备正在升级你的应用程序,所以checkDataBase()返回true,所以你不会调用copyDataBase()。因此,您使用的是以前的数据库,而不是generalSettings表。尝试解决这个问题

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
  copyDatabase();
}

and

public InstallDB(Context context, String name) {
    super(context, name, null, DB_VERSION); 
    // DB_VERSION is an int,update it every new build

    this.ctx = context;
    this.DBNAME = name;
    this.DBPATH = this.ctx.getDatabasePath(DBNAME).getAbsolutePath();
    Log.e("Path 1", DBPATH);

}

Let me know if it works and if not ??

如果有用的话,请告诉我。