2016-04-23 42 views
-2

下面是用於創建表遊客的商店名稱和地點ADRESS將值插入到SQLite數據庫時出錯?任何人都可以解決這個問題嗎?

公共類DbFav {

/** for database */ 
static final String DataBaseName = "Favdb"; 

/** for register table */ 
static final String favtable = "Visiters"; 
static final String VisitID = "visitid"; 


static final String ColNam="Name"; 

static final String ColAdd="Address"; 


public static final int DATABASE_VERSION = 3; 


private static final String REGISTER_TABLE_CREATE ="Create table " + favtable + "("+VisitID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + ColNam+ " VARCHAR(15)," + ColAdd+ " VARCHAR(15)) "; 
private final Context context; //for the linkage of the database wid the app 
private DatabaseHelper DBHelper; 
private SQLiteDatabase db; 

public DbFav(Context ctx){ 
    Log.i("test****", "**test***"); 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 
    private static class DatabaseHelper extends SQLiteOpenHelper{ 
    public DatabaseHelper(Context context){ 

     //super(context, DataBaseName , null, DATABASE_VERSION); 
     super(context, DataBaseName, null, DATABASE_VERSION); 
     Log.i("context","context"); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     db.execSQL(REGISTER_TABLE_CREATE); 
     //db.execSQL(SEARCH_TABLE_CREATE); 
     Log.i("************", "table created"); 
    } 



    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //for update 
     // TODO Auto-generated method stub 
     Log.w("tag", "Upgrading database from version " + oldVersion + " to "+ newVersion + ", which will destroy all old data"); 
     db.execSQL("DROP TABLE IF EXISTS " + favtable); 

     onCreate(db); 
    } 

} 

public DbFav open() throws SQLException{ 
    db = DBHelper.getWritableDatabase(); 
    Log.i("open", "message"); 
    return this; 
} 
public void close(){ 
    DBHelper.close(); 
} 


public long insert(String Name,String Address) { 
    Log.i("**** suruchitest **** ","*** test ***"); 

    ContentValues initialValues = new ContentValues(); 


    initialValues.put(ColNam,Name); //key , value 
    initialValues.put(ColAdd,Address); 
    Log.i("Values Inserted","Values are inserted"); 
    return db.insert(favtable, null, initialValues); 

值數據庫編碼從下方編碼保存到DB這給參觀者包含除外無上校命名(名稱,地址)

get_place=trname.getText().toString(); 
get_add=tradd.getText().toString(); 
db.open(); 
db.insert(get_place,get_add); 
db.close(); 
Error inserting Name=KT Royal Hotel Sangrur Address=Nankiana Chowk, Patiala Bypass Road, Sangrur, Punjab 148001, India 
android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20) 
+0

請把你這裏的錯誤logcat中...... – sushildlh

+0

我不知道,如果VARCHAR採用的是Android的SQLite的數據類型。 – allancth

+0

@allancth varchar是一個數據類型。沒有logcat錯誤很難找出錯誤。 – sushildlh

回答

1

錯誤狀態

[INSERT INTO Visiters(姓名,地址)VALUES(?,?)]數據類型不匹配

您已經定義Name INTEGER PRIMARY KEY AUTOINCREMENT是一個整數,並嘗試插入一個字符串進入桌子。

public long insert(String Name,String Address) { 
    [...] 
    initialValues.put(ColNam,Name); // Name should be an int 
    [...] 
} 

您需要正確定義您的表格。

注意:不要忘記在更改SQL方案後刪除數據以進行測試。

此外,您應該使用TEXT而不是VARCHAR(15)

SQLite不強加任何長度限制

+0

我已經編輯我的代碼,看到私人靜態最終字符串REGISTER_TABLE_CREATE =「創建表」 +「+」+「+」+「+」+「+」VARCHAR(15))「;欲瞭解更多信息,請參閱上面的編輯問題。 – Angel

+0

錯誤是在therse兩行返回db.insert(favtable,null,initialValues); \t \t \t \t \t db.insert(get_place,get_add); – Angel

+0

是的,謝謝它的作品。你能告訴我在哪裏看到這個插入的值嗎? – Angel

相關問題