安卓插入到SQLite数据库 [英] Android insert into sqlite database
问题描述
我知道有可能是一个简单的事情,我很想念,但我一直在打我的头撞墙,在过去两个小时。我对Android应用程序,我目前正在(安卓1.6版)的数据库,我只是想插入一条记录到数据库表。我的code如下所示:
I know there is probably a simple thing I'm missing, but I've been beating my head against the wall for the past hour or two. I have a database for the Android application I'm currently working on (Android v1.6) and I just want to insert a single record into a database table. My code looks like the following:
//Save information to my table
sql = "INSERT INTO table1 (field1, field2, field3) " +
"VALUES (" + field_one + ", " + field_two + ")";
Log.v("Test Saving", sql);
myDataBase.rawQuery(sql, null);
在MyDatabase的变量是一个SQLiteDatabase对象,可以从架构中的另一个表中选择数据的罚款。该储蓄似乎(在LogCat中没有任何错误),做工精细,但当我将数据库从设备和源码浏览器中打开新的记录是不存在的。我也尝试手动运行在源码浏览器的查询和工作正常。对于表1的表架构是_id,FIELD1,FIELD2,FIELD3。
the myDataBase variable is a SQLiteDatabase object that can select data fine from another table in the schema. The saving appears to work fine (no errors in LogCat) but when I copy the database from the device and open it in sqlite browser the new record isn't there. I also tried manually running the query in sqlite browser and that works fine. The table schema for table1 is _id, field1, field2, field3.
任何帮助将是很大的AP preciated。谢谢!
Any help would be greatly appreciated. Thanks!
推荐答案
您查询是无效的,因为你所提供的2值3列。你的原始查询应该是这样的:
Your query is invalid because you are providing 2 values for 3 columns. Your raw query should look like:
sql = "INSERT INTO table1 (field1, field2) " +
"VALUES (" + field_one + ", " + field_two + ")";
虽然你的架构包含三个字段。顺便说一句,你可以看到日志,看到sqlite的实际错误报告。
although your schema contains three fields. By the way, you can see the log to see the actual error reporting from sqlite.
这篇关于安卓插入到SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!