【问题标题】:Android: Sqlite Exception no such table?Android:Sqlite Exception 没有这样的表?
【发布时间】:2010-11-13 06:18:29
【问题描述】:

我已经决定我讨厌 SQL,为此我正在尝试创建一个帮助类,我可以反复重用它,而不必再弄乱它,但它不起作用!

这是我现在拥有的:

DBAssistant.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 compile: SELECT * FROM test

指向printTable()方法中的行:

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

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


编辑:

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

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

第二个为什么你不使用内容提供者和uri>已经是无SQL的概念?

什么...?我不知道。请解释一下那是什么,或者给我一个指向某种教程的链接。

【问题讨论】:

  • 如果异常说没有这个名字的表,那么它是真的,你用 adb 控制台工具检查了吗?第二个为什么你不使用已经是 SQLless 的内容提供者和 uri 概念?
  • 我也遇到了同样的情况。即使表的名称是正确的但仍然得到一个 SQLException。 (没有这样的表)
  • AFAIK,在某些情况下可能不需要内容提供者。

标签: android sqlite exception


【解决方案1】:

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

参考Dev Guide

【讨论】:

    【解决方案2】:

    由于您查询的列不存在,也可能引发此异常。例如,您确定您的表有一个 col _id,因为 android 期望找到它。

    【讨论】:

      【解决方案3】:

      创建数据库时发生错误。只需在创建表时调试您的代码,如果是,您将看到有错误

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-18
        • 1970-01-01
        • 2016-08-23
        • 2017-02-19
        • 2012-09-28
        相关资源
        最近更新 更多