如何使用rawQuery()方法插入记录? [英] How to use rawQuery() method to insert a record?

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

问题描述

我需要使用 rawQuery()方法存储记录,因为我想插入当前日期和时间( datetime()),但是我还需要插入包含引号的字符串.

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.

所以我写了这段代码:

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).

At this moment, the only method that works to insert a record (with quotes problem) is execSQL(String s).

推荐答案

SQLite没有本地日期时间数据存储类型.它可以存储字符串或日期的整数表示形式.

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

您的初次尝试几乎是正确的,但是您的datetime()函数调用需要一个'NOW'的参数.

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});

如果未插入所有值,则可以通过在查询中的表名之后插入字段列表来指定要插入数据的各个列

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'))";

另一个可能的选择是在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天全站免登陆