我创建了一个简单的数据库,用于将数据列表插入到我的示例dB中,但是当我想将数据发送到数据库时,它会给我这个错误
01-17 15:30:43.015:ERROR /数据库(822):在准备'INSERT INTO friends values(Sharma,SaiGeetha,18);“”'时,在0x276e50上失败1(没有这样的专栏:Sharma) / p>
供参考,我正在粘贴代码
sampleDB = this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
ArrayList<String>FirstName = new ArrayList<String>();
ArrayList<String>LastName = new ArrayList<String>();
ArrayList<Integer >Age = new ArrayList<Integer>();
FirstName.add("SaiGeetha");
FirstName.add("Vivek");
FirstName.add("Rahul");
LastName.add("Sharma");
LastName.add("Lilani");
LastName.add("Lami");
Age.add(18);
Age.add(20);
Age.add(23);
sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
SAMPLE_TABLE_NAME +
" (LastName VARCHAR, FirstName VARCHAR," +
" Age INT(3));");
for(int i=0;i<3;i++)
{
sampleDB.execSQL("INSERT INTO " +
enter code here
SAMPLE_TABLE_NAME +
" Values("+LastName.get(i)+","+FirstName.get(i)+","+Age.get(i)+");"+"\"\"");
}
/*sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Makam','Sai Geetha','India',25);");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Chittur','Raman','India',25);");
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values ('Solutions','Collabera','India',20);");*/
Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
SAMPLE_TABLE_NAME +
" where Age > 10 LIMIT 5", null);
if (c != null ) {
if (c.moveToFirst()) {
do {
String firstName = c.getString(c.getColumnIndex("FirstName"));
int age = c.getInt(c.getColumnIndex("Age"));
Log.e("---LIST FROM DATABASE--","---VALUE---"+firstName);
Log.e("---LIST FROM DATABASE--","---VALUE---"+age );
}while (c.moveToNext());
}
}
sampleDB.close();
} catch (SQLiteException se )
{
Log.e(getClass().getSimpleName(), "Could not create or Open the database");
} finally {
if (sampleDB != null)
sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
sampleDB.close();
}
}
答案 0 :(得分:3)
尝试:
sampleDB.execSQL("INSERT INTO " +
SAMPLE_TABLE_NAME +
" Values('"+LastName.get(i)+"','"+FirstName.get(i)+"',"+Age.get(i)+");");
而不是原件。您需要在SQL语句中使用引号传递String值。如果您不引用它们,则假定您引用columns
。
如njzk2所述,其他方法的使用更为合适。请看这个例子:http://learnandroid.blogspot.com/2008/01/android-database.html。特别是以下摘录:
public void createRow(String code, String name) {
ContentValues initialValues = new ContentValues();
initialValues.put("code", code);
initialValues.put("name", name);
db.insert(DATABASE_TABLE, null, initialValues);
}
答案 1 :(得分:0)
像这样修改你的SQL:
"INSERT INTO " + SAMPLE_TABLE_NAME + " (LastName, FirstName, Age) Values( "+LastName.get(i)+","+FirstName.get(i)+","+Age.get(i)+")"