2012-02-11 184 views
0

我想在android中創建一個數據庫,但我有這個問題,在(AVD)中擦除數據庫文件,重命名類「手機」中的數據庫,但沒有任何工作!SQLite數據庫

02-11 13:00:27.491: E/Database(487): android.database.sqlite.SQLiteException: table Phones_Table has no column named nam: , while compiling: INSERT INTO Phones_Table(nam, tel) VALUES(?, ?);

這是我的代碼:

public class Phones { 
    public static final String ID_ROW = "_id"; 
public static final String ID_PERSON = "nam"; 
public static final String ID_PHONE = "tel"; 

private static final String N_DB = "Phones"; 
private static final String N_TABLE = "Phones_Table"; 
private static final int VERSION_DB = 1; 

private DBHelper nHelper; 
private final Context nContext; 
private SQLiteDatabase nDB; 



private static class DBHelper extends SQLiteOpenHelper{ 

    public DBHelper(Context context) { 
     super(context, N_DB, null, VERSION_DB); 
     // TODO Auto-generated constructor stub 
    } 

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

    } 

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

} 

public Phones (Context c){ 

    nContext = c; 
} 

public Phones open() throws SQLException{ 
    nHelper = new DBHelper(nContext); 
    nDB = nHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    // TODO Auto-generated method stub 
    nHelper.close(); 
} 

public long createEntry(String nam, String tel) { 
    // TODO Auto-generated method stub 
    //insert data 
    ContentValues cv = new ContentValues(); 
    //put content of name 
    cv.put(ID_PERSON, nam); 
    //put content of Phone 
    cv.put(ID_PHONE, tel); 

    //return content in table 
    return nDB.insert(N_TABLE, null, cv); 

} 

public String receive() { 
    // TODO Auto-generated method stub 
    String[] columns = new String[]{ID_ROW, ID_PERSON, ID_PHONE}; 
    Cursor c = nDB.query(N_TABLE, columns, null, null, null, null, null); 
    String result = ""; 

    int iRow = c.getColumnIndex(ID_ROW); 
    int iName = c.getColumnIndex(ID_PERSON); 
    int iPhone = c.getColumnIndex(ID_PHONE); 
    //loop 
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){ 
     result = result + c.getString(iRow) + " " + c.getString(iName) + " " + c.getString(iPhone) + "\n "; 

    } 

    return result; 
}} 

真的會感謝你的幫助

回答

4
ID_PERSON + "TEXT NOT NULL, "+ 
      ^

你錯過了在create table字符串指定位置的空間。下一行同樣的東西。

您的表格將有名爲namTEXTtelTEXT的列,但沒有額外的空間。

+0

非常感謝你,墊,我修好了,不明白我怎麼沒看到! – JLouis 2012-02-11 13:45:23