我在sqlite数据库中有一个表,其中包含以下模式
CREATE TABLE os_logs (version STRING, user STRING, date STRING);
我将以下命令设置为名为cmd
的变量。
select count(*), version
from os_logs
group by version
order by version;
通过dbGetQuery发送该命令后,我得到了版本而不是字符串的数字结果。
db <- dbConnect(SQLite(),"./os_backup.db")
dbGetQuery(db,cmd)
count(*) version
1421 NA
1797 0.7
6 0.71
2152 0.71
1123 0.72
3455 1
2335 1
版本应为
0.70.1111_Product
0.71.22_Dev
0.71.33_Product
...
为什么我的sqlite数据库中的字符串被转换成R中的数字?如果我在sql cmd行上执行该命令,则它可以正常工作
编辑: 这是表格的创建方式。 (自从我在原始问题中对其进行了编辑以来,有了更多信息。
drop table vocf_logs;
CREATE TABLE vocf_logs (version STRING, driver STRING, dir STRING, uuid STRING PRIMARY KEY, t_start STRING);
CREATE TABLE log_os (uuid STRING PRIMARY KEY, os STRING);
.separator ","
.import vocf_dirs.csv vocf_logs
-- Put the OsVersion info from name_redacted into the table
UPDATE vocf_logs
SET version=(select log_os.os from log_os where uuid = vocf_logs.uuid);
答案 0 :(得分:2)
你描述的应该可以正常工作。您必须以不同方式执行某些操作或将其错误地插入到数据库中。
这是一步一步的测试,完全相同并且有效:
# Load package and connect
R> library(RSQLite)
R> db <- dbConnect(SQLite(),"./os_backup.db")
# Create db and insert data
R> dbSendQuery(db, "CREATE TABLE os_logs (version STRING, user STRING, date STRING);")
R> dbSendQuery(db, "INSERT INTO os_logs VALUES ('0.70.1111_Product', 'while', '2015-04-23')")
R> dbSendQuery(db, "INSERT INTO os_logs VALUES ('0.70.1111_Product', 'while', '2015-04-24')")
R> dbSendQuery(db, "INSERT INTO os_logs VALUES ('0.71.22_Dev', 'while', '2015-04-24')")
# Run query counting versions
R> dbGetQuery(db, "SELECT version, count(*) FROM os_logs GROUP BY version ORDER BY version;")
version count(*)
1 0.70.1111_Product 2
2 0.71.22_Dev 1
答案 1 :(得分:0)
创建原始表是错误的。 R中的方法是正确的。根据此处的数据类型说明:https://www.sqlite.org/datatype3.html。
The declared type of "STRING" has an affinity of NUMERIC, not TEXT.
当使用TEXT类型创建表时,它按预期工作。