如何使用SQL命令文件创建SQLite3数据库文件? [英] How can I create a SQLite3 database file using a SQL command file?

查看:433
本文介绍了如何使用SQL命令文件创建SQLite3数据库文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些SQL命令的文件,如下所示:

I have a file which contains some SQL commands, something that looks like this:

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

我想使用这些命令创建

如何在Ubuntu上执行此操作?

How do I do that on Ubuntu?

推荐答案

这不是一个SQL文件,包含一堆MySQL特定的东西,其中一些SQLite将接受,一些它不会。

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.

您不需要 create database 使用与SQLite。如果要在命令行中运行 sqlite3 时创建一个数据库:

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:

$ sqlite3 db_name.sqlt < your_sql.sql

如果 db_name.sqlt 存在那么它将被使用,如果它不存在,那么它将被创建。所以 create database 使用都是由你运行 sqlite3 。您可能需要根据Python想要查看的内容使用不同的扩展。

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.

引用的反引号是MySQLism,双引号是标识符的标准引用机制。

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.

SQLite不会知道 int(10)unsigned 意味着,你必须删除 unsigned ,SQLite才会接受它。 SQLite也不会知道 ENGINE = InnoDB DEFAULT CHARSET = utf8 是什么意思所以你必须删除它。

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.

你可能会遇到其他事情,MySQL很高兴,但SQLite不是。你必须尝试它,修复它,并尝试它,并修复它,直到它的工作。或者尝试找到一个可以在数据库之间翻译的工具,我总是手工做这些事情或使用一次性脚本,所以我不知道任何可以帮助你的工具。

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.

这篇关于如何使用SQL命令文件创建SQLite3数据库文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆