如何使用rawQuery插入记录 [英] How to use rawQuery to insert a record

查看:151
本文介绍了如何使用rawQuery插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 rawQuery 方法来存储一个记录,因为我想插入当前日期和时间(日期时间()),但我也需要插入包含双引号的字符串。

I need to store a record using rawQuery method, because i want to insert the current date and time (datetime()), but I also need to insert strings that contain quotes.

所以我写了这个code:

So I wrote this code:

String sql="INSERT INTO sms VALUES ( null, ?1, ?2, ?3, datetime())";
dbw.rawQuery(sql, new String[]{str1,str2,str3});

不过,这并不存储任何东西......怎么了?

But it doesn't store anything... what's wrong?

在这样,我没有得到错误,但没有插入的记录。

In this way I don't get errors, but the record is not inserted.

String mitt="mitt", dest="dest", text="text";
String sql="INSERT INTO sms VALUES ( null, ?, ?, ?, datetime('NOW'))";
dbw.rawQuery(sql, new String[]{mitt,dest,text});

此时,该工程中插入一条记录(带引号的问题)的唯一方法就是 execSQL(String s)将

推荐答案

SQLite没有一个本地datetime数据存储类型。它可以存储字符串或重的日期presentation一个整数代替。

SQLite doesn't have a native datetime data storage type. It can store a string or an integer representation of a date instead.

要转换成你的价值观,你可以使用 sqlite的日期和时间函数的文档详细介绍了日期时间函数

To convert your values you can use the date time functions detailed in Sqlite Date and Time functions documentation

您最初的尝试几乎是正确的,但你的日期时间()函数调用需要的参数现在

Your initial attempt is almost correct, but your datetime() function call requires an argument of 'NOW'.

String sql="INSERT INTO sms VALUES ( null, ?, ?, ?, datetime('NOW'))";

另外,你应该叫 execSQL 而不是 rawQuery 这是期待返回记录集。

Also you should call execSQL instead of rawQuery which is expecting to return a recordset.

dbw.execSQL(sql, new String[]{str1,str2,str3});

您可以通过ALO表名后插入一个字段列表中查询,如果没有插入所有的值,指定单个列将数据插入到

You can alo specify individual columns to insert data into by inserting a field list after the table name in your query if not inserting all the values

String sql = "INSERT INTO sms(f1, f2, f3, f4)"
           + "VALUES ( null, ?, ?, ?, datetime('NOW'))";

这可以是可能的另一种选择是<一个href="http://stackoverflow.com/questions/200309/sqlite-database-default-time-value-now/200329#200329">using SQLite中默认的时间戳,虽然我没有在Android的尝试这个。

Another option that may be possible is using a default timestamp in SQLite ,although I have not attempted this in android.

这篇关于如何使用rawQuery插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆