当前位置:首页 > 数据库 > SQlite

Android:Sqlite Exception没有这样的表?

我已经决定讨厌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 compiling: SELECT * FROM test

指向printTable()方法中的行:

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

这让我感到困惑,因为在此之前多次调用getColNames()方法中使用了同一行代码,并且没有问题.另外,如果该表不存在,当我调用insertRow()时会不会引发异常?我连续五次调用该方法,没有抛出任何异常!这是怎么回事

编辑:

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.


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!