Android 将数据插入SQLite时出错
使用@hotmail插入数据时,我在插入数据时遇到问题 这就是错误所在Android 将数据插入SQLite时出错,android,sqlite,syntax,insert,android-sqlite,Android,Sqlite,Syntax,Insert,Android Sqlite,使用@hotmail插入数据时,我在插入数据时遇到问题 这就是错误所在 11-28 08:07:19.684: E/CREATE TABLE ERROR(21840): android.database.sqlite.SQLiteException: near "@hotmail": syntax error: , while compiling: INSERT INTO user_table SELECT han AS userName, 123 AS password, han@hotm
11-28 08:07:19.684: E/CREATE TABLE ERROR(21840): android.database.sqlite.SQLiteException: near "@hotmail": syntax error: , while compiling: INSERT INTO user_table SELECT han AS userName, 123 AS password, han@hotmail.com AS emailAddress, 0123456678 AS phoneNuber, 50.00 AS balance UNION SELECT felicia, 123, felicia@hotmail.com, 0123456678, 100.00
但是当我删除@hotmail时,我遇到了另一个问题,没有这样的专栏
11-28 08:16:06.704: E/CREATE TABLE ERROR(24403): android.database.sqlite.SQLiteException: no such column: felicia: , while compiling: INSERT INTO user_table SELECT han AS userName, 123 AS password, han AS emailAddress, 0123456678 AS phoneNuber, 50.00 AS balance UNION SELECT felicia, 123, felicia, 0123456678, 100.00
这是我的桌子
String createuser = "CREATE TABLE user_table (_id integer primary key autoincrement not null, "
+ "userName text,"
+ "password text,"
+ "emailAddress text,"
+ "phoneNumber text,"
+ "balance text);";
这是我的数据插入
String data = " INSERT INTO user_table SELECT han AS userName, "
+ " 123 AS password, "
+ " han@hotmail.com AS emailAddress, "
+ " 0123456678 AS phoneNuber, "
+ " 50.00 AS balance "
+ " UNION SELECT felicia, 123, felicia@hotmail.com, 0123456678, 100.00 ";
try
{
db.execSQL(createuser);
db.execSQL(data);
}
catch(Exception e)
{
Log.e("CREATE TABLE ERROR", e.toString());
e.printStackTrace();
}
您的语法错误,请在引号中插入文本
'text GOES HERE'
,您错过了一个,
呸。作为[潜在的]开发人员,您需要自己解决语法问题
这里有两个重要步骤
sqlite3.exe
,Android上提供的任何东西(有很多!)或类似的东西。我将使用SQLFiddle,这样我们都可以参与,哇哦
下一步是理解语法。的语法与图形化列车轨道进行了很好的记录。(免费:ANSI SQL语法中的文本文字写得像“字符串”。)
话虽如此,你看。随便玩吧
CREATE TABLE user_table (
_id integer primary key autoincrement not null,
userName text,
password text,
emailAddress text,
phoneNumber text,
balance text);
-- INSERT from a select
-- This REQUIRES that the column as specified after the table name
insert into user_table (userName, password, emailAddress, phoneNumber, balance)
select
'han' as userName,
123 AS password,
'han@hotmail.com' AS emailAddress,
'0123456678' AS phoneNuber,
50.00 AS balance;
-- Insert from VALUES, if they match and are in-order, do not
-- need the column names.
-- Insert NULL into _id to get the auto-increment behavior.
insert into user_table
values (NULL, 'han2', 123, 'han@hotmail.com', '0123456678', 50.00);
-- Multi-insert from SELECT (could also be done with VALUES)
insert into user_table (userName, password, emailAddress, phoneNumber, balance)
select
'han3' as userName,
123 AS password,
'han@hotmail.com' AS emailAddress,
'0123456678' AS phoneNuber,
50.00 AS balance
union select
'han4' as userName,
123 AS password,
'han@hotmail.com' AS emailAddress,
'0123456678' AS phoneNuber,
50.00 AS balance
现在,虽然这在直接SQL查询中起作用,但当将工作查询移植到Android上时,您需要适应占位符的使用-也就是说,SQL命令看起来像是插入到用户表值中的(NULL、、?、?、?、?)
(其中每个?
都是占位符)然后,将向使用SQL字符串的适当方法调用提供一个值数组
Android还支持一些基本的表单,比如非常有用的表单,并且可以处理很多“常见的垃圾”。的文档建议不要将execSQL用于基本的插入/更新/删除操作,这对于常见场景是一个很好的建议-在一个事务中多次调用
INSERT
,与更复杂的手动多次插入一样“好”(从代码维护的角度来看,可能更好)。-1。请参阅上一个问题中的注释,了解我关于如何编写和测试查询的建议。在没有立即反馈的情况下盲目猜测错误是什么(就像您从CLI中得到的那样),并不是成功的关键。另外,在Android中编写“for real”查询时,请使用占位符。由于几个原因(包括回归),这仍然是不正确的。感谢您的指导。