如何使用 SQL 命令文件创建 SQLite3 数据库文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14005731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I create a SQLite3 database file using a SQL command file?
提问by user1639431
I have a file which contains some SQL commands, something that looks like this:
我有一个包含一些 SQL 命令的文件,如下所示:
CREATE DATABASE IF NOT EXISTS `db_name`;
USE `db_name`;
CREATE TABLE IF NOT EXISTS `customers` (
`id` int(10) unsigned NOT NULL,
`f_name` varchar(50) DEFAULT NULL,
`l_name` varchar(50) DEFAULT NULL,
`company_name` varchar(200) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`phone` varchar(25) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `customers` (`id`, `f_name`, `l_name`, `company_name`, `email`, `phone`) VALUES
...
...
...
I'd like to use these commands to create an SQLite3 database file in order to use it easily with Python.
我想使用这些命令来创建一个 SQLite3 数据库文件,以便在 Python 中轻松使用它。
How do I do that on Ubuntu?
我如何在 Ubuntu 上做到这一点?
回答by mu is too short
That isn't quite an SQL file, that contains a bunch of MySQL-specific stuff some of which SQLite will accept and some it won't. We'll start at the top.
这不是一个 SQL 文件,它包含一堆特定于 MySQL 的东西,其中一些 SQLite 会接受,而另一些则不会。我们将从顶部开始。
You don't need create database
or use
with SQLite. If you want to create a database just name it when you run sqlite3
from the command line:
您不需要create database
或use
使用 SQLite。如果要创建数据库,只需sqlite3
在从命令行运行时为其命名:
$ sqlite3 db_name.sqlt < your_sql.sql
If db_name.sqlt
exists then it will be used, if it doesn't exist then it will be created. So create database
and use
are implied by how you run sqlite3
. You might need to use a different extension depending on what Python wants to see.
如果db_name.sqlt
存在则将使用它,如果它不存在则将被创建。所以create database
和use
是由你的运行方式所暗示的sqlite3
。根据 Python 想要查看的内容,您可能需要使用不同的扩展。
The backticks for quoting are a MySQLism, double quotes are the standard quoting mechanism for identifiers. Lucky for you, SQLite will accept them so you can leave them alone.
引号的反引号是 MySQL 主义,双引号是标识符的标准引用机制。幸运的是,SQLite 会接受它们,因此您可以不理会它们。
SQLite won't know what int(10) unsigned
means, you'll have to remove the unsigned
before SQLite will accept it. SQLite also won't know what ENGINE=InnoDB DEFAULT CHARSET=utf8
means so you'll have to remove that as well.
SQLite 不知道是什么int(10) unsigned
意思,您必须unsigned
在 SQLite 接受它之前删除它。SQLite 也不知道是什么ENGINE=InnoDB DEFAULT CHARSET=utf8
意思,所以你也必须删除它。
You'll probably run into other things that MySQL is happy with but SQLite is not. You'll have to try it and fix it and try it and fix it until it works. Or try to find a tool that can translate between databases for you, I always do these sorts of things by hand or using one-off scripts so I don't know of any tools that can help you.
您可能会遇到 MySQL 满意但 SQLite 不满意的其他事情。您必须尝试并修复它,然后尝试并修复它,直到它起作用为止。或者尝试找到一种可以为您在数据库之间进行转换的工具,我总是手动或使用一次性脚本来完成这些事情,所以我不知道有任何工具可以帮助您。
回答by ravdhaw
Basically above commands are for mysql or other database (most of these have to be tweaked in order to work with sqlite. Sqlite stores database in the form of file. Basically when you start sqlite it will create a file (if not present). You can create or open a database by typing
基本上上面的命令是针对 mysql 或其他数据库的(其中大部分必须进行调整才能与 sqlite 一起使用。Sqlite 以文件的形式存储数据库。基本上当你启动 sqlite 时,它会创建一个文件(如果不存在)。你可以通过键入创建或打开数据库
sqlite3 db
on command line. This statement create or open database named "db"
在命令行上。此语句创建或打开名为“db”的数据库