以下是用于创建表格访问者以存储名称和地点地址的数据库编码
公共课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);
从以下编码保存到数据库的值,其中访问者的例外不包含名为(名称,地址)的列
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 :(得分: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不会施加任何长度限制