2012-03-14 39 views
1

我正在使用我的sqldatabase,但我無法繼續前進,因爲我的程序給了我一個錯誤信息,即使該列在「KEY_NAME」下的代碼頂部明確定義,也沒有這樣的列。如果任何人都可以提供幫助我還是需要我再發布代碼,請告訴我謝謝! 我只發佈了我的數據庫。SQL數據庫列錯誤或不存在?

** * ****最近logcat的信息 1月3日至一十四日:24:54.285:E /數據庫(26073):android.database.sqlite.SQLiteException:表PeopleTable沒有列在編譯時命名爲persons_name:,INSERT INTO PeopleTable(persons_hotness,persons_name)VALUES(?,?);


public class HotOrNot { 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_NAME = "persons_name"; 
    public static final String KEY_HOTNESS = "persons_hotness"; 

    private static final String DATABASE_NAME = "HotOrNotDB"; 
    private static final String DATABASE_TABLE = "PeopleTable"; 
    private static final int DATABASE_VERSION = 1; 

    private DbHelper ourHelper; 
    private final Context ourContext; 
    private SQLiteDatabase ourDatabase; 

    private static class DbHelper extends SQLiteOpenHelper { 
     public DbHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 
      db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID 
        + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME 
        + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);"); 

      // String sql = String.format("create table %s " 
      // + "(%s int primary key, %s int, %s text, %s text)", 
      // DATABASE_TABLE, KEY_ROWID, KEY_NAME, KEY_HOTNESS); 
      // db.execSQL(sql); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE); 
      onCreate(db); 
     } 
    } 

    public HotOrNot(Context c) { 
     ourContext = c; 
    } 

    public HotOrNot open() throws SQLException { 
     ourHelper = new DbHelper(ourContext); 
     ourDatabase = ourHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     ourHelper.close(); 
    } 

    public long createEntry(String name, String hotness) { 
     // TODO Auto-generated method stub 
     ContentValues cv = new ContentValues(); 
     cv.put(KEY_NAME, name); 
     cv.put(KEY_HOTNESS, hotness); 
     return ourDatabase.insert(DATABASE_TABLE, null, cv); 
    } 

    public String getData() { 
     // TODO Auto-generated method stub 
     String[] columns = new String[] { KEY_ROWID, KEY_NAME, KEY_HOTNESS }; 
     Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, 
       null, null); 
     String result = ""; 
     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iHotness = c.getColumnIndex(KEY_HOTNESS); 

     for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { 
      result = result + c.getString(iRow) + " " + c.getString(iName) 
        + " " + c.getString(iHotness) + "\n"; 
     } 
     return result; 
    } 
} 
+0

仍在尋找一個有效的答案:\ – 2012-04-01 19:08:37

回答

2

嘗試+ KEY_NAME +更換+ "KEY_NAME" +沒有雙引號)。這條線路是內public void onCreate(SQLiteDatabase db)

+0

感謝您的快速反應和良好的發現,但我仍然得到類似的logcat錯誤,即時尋找我的錯別字,但找不到任何 – 2012-03-14 05:28:23

+0

考慮創建數據庫再次。卸載並安裝應用程序。 – dira 2012-03-14 06:05:23

2

db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID 
      + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME 
      + " TEXT NOT NULL, " + KEY_HOTNESS + " TEXT NOT NULL);"); 
+0

+1現有代碼使用名爲「KEY_NAME」的列創建表,而不是變量KEY_NAME的內容。 – 2012-03-14 09:28:04

3

db.execSQL取代你db.execSQL查詢( 「CREATE TABLE」 + DATABASE_TABLE + 「(」 + KEY_ROWID +「INTEGER PRIMARY KEY AUTOINCREMENT,KEY_NAME TEXT NOT NULL,「+ KEY_HOTNESS +」TEXT NOT NULL);「);

嘗試上面的更改..我認爲問題可能是由於在KEY_NAME之前添加了空間。希望這可以幫助你..

+0

它沒有工作:( – 2012-03-14 21:17:00

相關問題