How can I create a SQLite3 database file using a SQL command file?

后端 未结 2 1241
没有蜡笔的小新
没有蜡笔的小新 2020-12-29 04:32

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          


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-12-29 05:18

    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.

    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
    

    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.

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

    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.

提交回复
热议问题