2010-11-13 61 views
3

我已經決定,我討厭SQL,爲此我試圖做一個幫助器類,我可以反覆使用一遍又一遍,永遠不必再惹它,但它不工作!Android:Sqlite異常沒有這樣的表?

這是我現在所擁有的:

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:沒有這樣的表:試驗:,在編譯:SELECT * FROM測試

指着printTable()方法行:

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

這有我這麼困惑,因爲代碼,同一條線路在getColNames()方法是在此之前多次打電話,並沒有問題跑使用。另外,如果表格不存在,當我撥打insertRow()時它不會引發異常?我連續調用了5次該方法,沒有拋出任何異常!這裏發生了什麼?


編輯:

在DBOpenHelper實現的onCreate()。把那裏的createTable()方法的內容。

爲什麼有必要在onCreate()方法中創建表格?如果我這樣做,我能夠使用我現有的方法這樣做,只是從這種方法調用它?

而第二個爲什麼你不使用內容提供者和uri>這個概念已經是SQLless?

Wha ...?我不知道。請解釋一下是什麼,或者給我一個指向某種教程的鏈接。

+0

如果異常表示沒有包含此名稱的表,那麼它是真的,您是否使用adb控制檯工具?第二個爲什麼你不使用已經無SQL的內容提供者和uri概念? – endryha 2010-11-13 07:31:22

+0

我也得到了同樣的情況。即使表名是正確的,但仍然有SQLException。 (無此表) – 2013-08-15 06:30:47

+0

AFAIK,在某些情況下可能不需要內容提供商。 – 2013-08-15 06:32:15

回答

2

在您的DBOpenHelper中執行onCreate()。把那裏的內容createTable()方法。

參見Dev Guide以供參考。

1

由於您查詢不存在的列,因此也可能引發此異常。例如,你確定你的表有一個col _id,因爲android期望找到它。

0

創建數據庫時發生錯誤。只需在創建表格時調試代碼,並且如果是這樣,您將看到有錯誤