简单的 SQL Lite 表/导入问题

发布于 2024-09-10 06:34:45 字数 299 浏览 2 评论 0原文

我有一个简单的 SQL 问题。我想创建一个 3 列数据库,并且有以下代码:

sqlite3 meshdb.db "create table t1 (t1key INTEGER PRIMARY KEY, prideID, pubmedID);"

当我尝试导入包含两列(prideID 和 pubmedID)的简单 csv 文件时,出现“预期有 3 列数据,但发现了 2 列”错误。我希望 t1key 是一个整数,并在添加新字段时自动计数。我是否必须将 NOT NULL 放在 PRIMARY KEY 前面才能使其工作?

I have a simple SQL question. I want to make a 3 column database and I have the following code:

sqlite3 meshdb.db "create table t1 (t1key INTEGER PRIMARY KEY, prideID, pubmedID);"

When I try to import a simple csv file with two columns (prideID and pubmedID), I get a "expected 3 columns of data but found 2" error. I want the t1key to be an integer, and automatically count up as new fields are added. Do I have to put NOT NULL in front of PRIMARY KEY to for this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑饮青盏花 2024-09-17 06:34:46

.import 不支持重塑输入(设置分隔符除外)。您需要将 CSV 文件导入到临时表中,然后将其插入到真实表中。下面是一个示例会话:

$ cat a.csv 
1,2
3,4
5,6
$ sqlite3 a.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo(id integer primary key,x,y);
sqlite> create temp table footmp(x,y);
sqlite> .separator ,
sqlite> .import a.csv footmp
sqlite> select * from footmp;
1,2
3,4
5,6
sqlite> insert into foo(x,y) select * from footmp; 
sqlite> select * from foo; 
1,1,2
2,3,4
3,5,6
sqlite> drop table footmp; 

您会看到 ID 已被计数。这是因为类型为 INTEGER PRIMARY KEY 的列被视为内部 ROWID 的别名 - 它始终是唯一的升序数字。

.import does not support reshaping the input (except from setting the separator). You need to import the CSV file into a temporary table and the insert that into the real table. Here is a example session:

$ cat a.csv 
1,2
3,4
5,6
$ sqlite3 a.db
SQLite version 3.6.23.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table foo(id integer primary key,x,y);
sqlite> create temp table footmp(x,y);
sqlite> .separator ,
sqlite> .import a.csv footmp
sqlite> select * from footmp;
1,2
3,4
5,6
sqlite> insert into foo(x,y) select * from footmp; 
sqlite> select * from foo; 
1,1,2
2,3,4
3,5,6
sqlite> drop table footmp; 

You see that ID is counted up. This is because a column with type INTEGER PRIMARY KEY is treated as an alias for the internal ROWID - which always is a unique, ascending number.

你没皮卡萌 2024-09-17 06:34:46

而不是 insert 使用

create table newtable as select  * from  footmp;

更好更快。

Instead of insert use

create table newtable as select  * from  footmp;

It is better and quicker.

七堇年 2024-09-17 06:34:46

使用我使用的 sqlite .version (SQLite 3.7.15.2 2013-01-09),您不必先导入到临时表中。我所做的只是确保在开始导入之前设置了分隔符。我确实指定了每行的 id 值,因此,csv 中的第一列是一组唯一的整数

With the .version of sqlite (SQLite 3.7.15.2 2013-01-09) I'm on, you don't have to import into a temp table first. All I did was to make sure I set a separator before starting the import. I did specify the id values for each row, so, my first column in the csv was a set of unique integers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文