从SQLite导出数据到SQL Server

37

有没有工具可以将 SQLite 数据库迁移到 SQL Server(包括结构和数据)?

6个回答

47

SQLite提供了一个.dump选项,可在命令行中运行。虽然我更喜欢使用SQLite数据库浏览器应用程序来管理SQLite数据库。您可以将结构和内容导出为可被几乎任何工具读取的.sql文件。选择 文件 > 导出 > 数据库为 SQL 文件。


3
导出的文件确实需要进行一些调整以适应语法上的差异,但并不太复杂。谢谢。 - Geoff Appleford
1
从技术上讲,SQLite shell并不是SQLite本身,而是与SQLite链接的控制台应用程序,它只是一个库。 - Benoit
4
这指引我朝正确的方向,但我正在寻找语法,所以我会在这里包含它:sqlite3 my_db.db -batch ".dump" > my_db.sql - Aaron R.
在我的情况下,这是复杂的......一个字段包含日期和时间,当导出时,它使用毫秒进行导出,当我尝试在SQL Server中执行时不兼容 :-( - jstuardo
这个 SQL 文件里有很多错误,我需要提交一个新的问题吗?还是我做错了什么? - Shayan
显示剩余3条评论

13

我知道这是一个旧的帖子,但我认为这个解决方案也应该在这里。

  • 安装SQLite的ODBC驱动程序
  • 运行odbcad32以获取x64或C:\Windows\SysWOW64\odbcad32.exe以获取x86
  • 创建SYSTEM DSN,选择SQLite3 ODBC Driver
  • 然后填写表格,其中数据库名称是指向sqlite数据库的文件路径

然后在SQL Server下以sysadmin身份运行

USE [master]
GO
EXEC sp_addlinkedserver 
   @server     = 'OldSQLite', -- connection name
   @srvproduct = '',          -- Can be blank but not NULL
   @provider   = 'MSDASQL', 
   @datasrc    = 'SQLiteDNSName' -- name of the system DSN connection 
GO

然后,您可以以普通用户的身份运行查询,例如:

SELECT * INTO SQLServerDATA FROM openquery(SQLiteDNSName, 'select * from SQLiteData')

或者您可以像这个那样,用于处理较大的表格。


SELECT * INTO SQLServerDATA FROM openquery(OldSQLite, 'select * from SQLiteData')你可以安装64位驱动程序并创建64位ODBC(odbcad32在system32文件夹中 - SQLite3 ODBC驱动程序) - R.Alonso

9

SQLite的.dump命令将整个数据库的内容输出为ASCII文本文件。该文件采用标准SQL格式,因此可以导入到任何SQL数据库中。 更多详细信息请参见此页面:sqlite3


1
好的,单独使用.dump命令只会输出到屏幕上。您还需要使用.output命令将其输出到文件中。 - aris

8

这个插件似乎已经失效了,Firefox 57.0 版本显示它不兼容且无法使用... - me_

0

针对Android。

adb root
adb shell
cd /data/com.xxx.package/databases/
sqlite3 db_name .dump >dump.sql

0
一个想法是这样做: - 在 SQL Lite 中查看模式并获取 CREATE TABLE 命令。 - 在 SQL SERVER 中执行,解析 SQL。 - 遍历数据为每一行创建 INSERT 语句。(也解析 SQL)
这段代码是测试版,因为它没有检测数据类型,也没有使用 @parameter 和 command 对象,但可以运行。
(您需要插入引用并安装 System.Data.SQLite;)
C#: 将此代码(或必要的代码)插入头部 cs using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Data.SQLite; using System.Threading; using System.Text.RegularExpressions; using System.IO; using log4net; using System.Net;
    public static Boolean SqLite2SqlServer(string sqlitePath, string connStringSqlServer)
    {
        String SqlInsert;
        int i;
        try
        {

            string sql = "select * from sqlite_master where type = 'table' and name like 'YouTable in SQL'";
            string password = null;
            string sql2run;
            string tabla;
            string sqliteConnString = CreateSQLiteConnectionString(sqlitePath, password);
            //sqliteConnString = "data source=C:\\pro\\testconverter\\Origen\\FACTUNETWEB.DB;page size=4096;useutf16encoding=True";

            using (SQLiteConnection sqconn = new SQLiteConnection(sqliteConnString))
            {



                sqconn.Open();

                SQLiteCommand command = new SQLiteCommand(sql, sqconn);
                SQLiteDataReader reader = command.ExecuteReader();

                SqlConnection conn = new SqlConnection(connStringSqlServer);
                conn.Open();
                while (reader.Read())
                {
                    //Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
                    sql2run = "" + reader["sql"];
                    tabla = "" + reader["name"];

                    /*
                    sql2run = "Drop table " + tabla;
                    SqlCommand cmd = new SqlCommand(sql2run, conn);                       
                    cmd.ExecuteNonQuery();
                    */



                    sql2run = sql2run.Replace("COLLATE NOCASE", "");
                    sql2run = sql2run.Replace(" NUM", " TEXT");
                    SqlCommand cmd2 = new SqlCommand(sql2run, conn);
                    cmd2.ExecuteNonQuery();


                    // insertar los datos.
                    string sqlCmd = "Select *  From " + tabla;
                    SQLiteCommand cmd = new SQLiteCommand(sqlCmd, sqconn);
                    SQLiteDataReader rs = cmd.ExecuteReader();
                    String valor = "";
                    String Valores = "";
                    String Campos = "";
                    String Campo = "";
                    while (rs.Read())
                    {
                        SqlInsert = "INSERT INTO " + tabla;
                        Campos = "";
                        Valores = "";
                        for ( i = 0; i < rs.FieldCount ; i++)
                        {

                            //valor = "" + rs.GetString(i);
                            //valor = "" + rs.GetName(i);
                            Campo = "" + rs.GetName(i);
                            valor = "" + rs.GetValue(i);

                            if (Valores != "")
                            {
                                Valores = Valores + ',';
                                Campos = Campos + ',';
                            }
                            Valores = Valores + "'" + valor + "'";
                            Campos = Campos + Campo;
                        }
                        SqlInsert = SqlInsert + "(" + Campos + ") Values (" + Valores + ")";
                        SqlCommand cmdInsert = new SqlCommand(SqlInsert, conn);
                        cmdInsert.ExecuteNonQuery();


                    }


                }

                }
            return true;
        } //END TRY
        catch (Exception ex)
        {
            _log.Error("unexpected exception", ex);

            throw;

        } // catch
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接