gpt4 book ai didi

Android: Sqlite Exception 没有这样的表?

转载 作者:太空狗 更新时间:2023-10-29 12:58:01 25 4
gpt4 key购买 nike

我已经决定我讨厌 SQL,为此我正在尝试制作一个我可以反复重用并且永远不必再弄乱它的辅助类,但它不起作用!

这是我现在拥有的:

数据库助手.java

    public class DBAssistant {
SQLiteDatabase db = null;


public DBAssistant (){
}

/** Opens database **/
public void openDB(Context context, String name){
db = new DBOpenHelper(context, name, DB_VERSION);
}

/** Creates the table "tableName" with the columns contained in "cols" **/
public void createTable(String tableName, String[] cols){
String table = "CREATE TABLE IF NOT EXISTS " + tableName + " (";
int numOfColums = cols.length;
columnNames = cols;
for (int i = 0; i < (numOfColums - 1); i++){
table += cols[i] + " VARCHAR, ";
}
table += cols[numOfColums - 1] + " VARCHAR);";
try{
db.execSQL("" + table);
System.out.println("ExecSQL:" + table);
} catch(Exception e){
System.out.println(e);
}
System.out.println("Column names: ");
for (String c : getColNames(tableName)){
System.out.print(c + " ");
}
}

/** Inserts "data" into new row **/
public void insertRow(String tableName, String[] data){
int cols = getCols(tableName);

System.out.println("if ((data.length) = " + (data.length) + ") ==" +
" (getCols(tableName) = " + getCols(tableName) + ")?");
if (data.length == cols){
System.out.println("Inside if loop");
String cmd = "INSERT INTO " + tableName + " (";
for (int i = 0; i < cols - 1; i++){
cmd += getColNames(tableName)[i] + ", ";
}
cmd += getColNames(tableName)[cols - 1] + ") VALUES ('";
for (int k = 0; k < data.length - 1; k++){
cmd += data[k] + "', '";
}
cmd += "');";
System.out.println(cmd);
db.execSQL(cmd);
}
else{
System.out.println("Inside else loop");
String dat = "";
String sCols = "";
for (String d : data)
dat += "'" + d + "'";
for (String c : getColNames(tableName))
sCols += "'" + c + "'";
System.out.println("-------------------");
System.out.println("[insertRow] ERROR: Number of elements in data[" + dat + "]");
System.out.println("doesnt match the number of columns [" + cols + "] in " + tableName);
System.out.println("-------------------");
}
}

/** Return String[] containing the column names in tableName **/
public String[] getColNames(String tableName){
Cursor c = db.rawQuery("SELECT * FROM " + tableName , null);
return c.getColumnNames();
}

/** Returns the number of rows in tableName **/
public int getCols(String tableName){
return getColNames(tableName).length;
}

/*** Other methods that have no relevance here .... ***/

private static class DBOpenHelper extends SQLiteOpenHelper {

DBOpenHelper(Context context, String dbName, int dbVersion) {
super(context, dbName, null, dbVersion);
}

@Override
public void onCreate(SQLiteDatabase arg0) {
// Do Nothing
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// Do Nothing
}
}
}

我已经包含了几个 System.out.println() 语句来帮助我调试代码和发现问题,所以请忽略它们。 PrintDB 是我编写的另一个测试,我用它来确保一切正常。现在,这就是我为确保一切正常而编写的内容....

DBAssistant db = new DBAssistant();
String dbName = "testing";
String tableName = "test";
String[] cols = {"_num", "num_eng", "num_span"};
String[] row1 = {"1", "one", "uno"};
String[] row2 = {"2", "two", "dos"};
String[] row3 = {"3", "three", "tres"};
String[] row4 = {"4", "four", "quatro"};
String[] row5 = {"5", "five", "cinco"};
TextView databaseView = (TextView)findViewById(R.id.databaseView);

db.openDB(this, dbName);
db.createTable(tableName, cols);
db.insertRow(tableName, row1);
db.insertRow(tableName, row2);
db.insertRow(tableName, row3);
db.insertRow(tableName, row4);
db.insertRow(tableName, row5);
databaseView.setText(db.printTable(tableName));

运行这段代码一切顺利,所有 System.out.println() 语句中都有正确的信息,直到它到达 databaseView.setText(db.printTable(tableName));部分并抛出异常

ERROR/AndroidRuntime(3440): android.database.sqlite.SQLiteException: no such table: test: , while compiling: SELECT * FROM test

指向 printTable() 方法中的行:

Cursor c = dbr.rawQuery("SELECT * FROM " + tableName , null);

这让我很困惑,因为在 getColNames() 方法中使用了同一行代码,在此之前多次调用该方法并且运行没有问题。另外,如果该表不存在,当我调用 insertRow() 时它不会抛出异常吗?我连续调用该方法 5 次,没有抛出任何异常!这里发生了什么?


编辑:

Implement onCreate() in your DBOpenHelper. Put there content of the createTable() method.

为什么需要在onCreate() 方法中创建表?如果我要这样做,我是否可以使用我现有的方法来这样做并从这个方法中调用它?

And the second why you don't use content providers and uri >concept which is already SQLless?

什么……?我不知道。请解释那是什么,或者给我留下某种教程的链接。

最佳答案

在您的 DBOpenHelper 中实现 onCreate()。将 createTable() 方法的内容放在那里。

参见 Dev Guide供引用。

关于Android: Sqlite Exception 没有这样的表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4171271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com