Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:Sqlite异常没有这样的表?_Android_Sqlite_Exception - Fatal编程技术网

Android:Sqlite异常没有这样的表?

Android:Sqlite异常没有这样的表?,android,sqlite,exception,Android,Sqlite,Exception,我已经决定,我讨厌SQL,为此,我正试图创建一个帮助器类,我可以反复使用,再也不必弄乱它,但它不起作用 以下是我现在拥有的: DBAssistant.java public class DBAssistant { SQLiteDatabase db = null; public DBAssistant (){ } /** Opens database **/ public void

我已经决定,我讨厌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()语句中都包含正确的信息,直到到达
databaseView.setText(db.printTable(tableName))
然后它抛出一个异常

错误/AndroidRuntime(3440):android.database.sqlite.SQLiteException:无此类表:测试:,编译时:从测试中选择*

指向
printTable()
方法中的行:

Cursor c = dbr.rawQuery("SELECT * FROM " + tableName , null);
这让我非常困惑,因为在
getColNames()
方法中使用了同一行代码,在此之前多次调用该方法,并且运行时没有问题。另外,如果表不存在,当我调用
insertRow()
时,它不会抛出异常吗?我连续调用了该方法5次,没有抛出一个异常!这里发生了什么?


编辑:

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

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

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

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

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


请参阅以获取参考。

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

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

如果异常显示没有具有此名称的表,则它将被删除;是的,你用adb控制台工具检查过了吗?第二个原因是为什么不使用内容提供者和uri概念,这已经是无SQL的了?我也有同样的情况。即使表的名称正确,但仍然得到了SQLException。(无此表)AFAIK,在某些情况下可能不需要内容提供商。
Cursor c = dbr.rawQuery("SELECT * FROM " + tableName , null);