简单的 SQL Lite 表/导入问题
我有一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.import
不支持重塑输入(设置分隔符除外)。您需要将 CSV 文件导入到临时表中,然后将其插入到真实表中。下面是一个示例会话:您会看到 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: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.
而不是
insert
使用更好更快。
Instead of
insert
useIt is better and quicker.
使用我使用的 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