34

是否有将SQLite数据库迁移到SQL Server(结构和数据)的工具?

4

6 回答 6

41

SQLite确实有一个 .dump 选项可以在命令行运行。虽然我更喜欢使用SQLite Database Browser应用程序来管理 SQLite 数据库。您可以将结构和内容导出到几乎任何东西都可以读取的 .sql 文件。文件 > 导出 > 数据库到 SQL 文件。

于 2008-10-02T15:48:54.843 回答
12

我知道这是旧线程,但我认为这个解决方案也应该在这里。

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

然后在 sysadmin 下运行 SQL Server

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')

或者您可以将类似的东西用于更大的桌子。

于 2015-10-05T12:41:51.107 回答
9

SQLite.dump命令会将数据库的全部内容输出为 ASCII 文本文件。此文件为标准 SQL 格式,因此可以导入任何 SQL 数据库。此页面上的更多详细信息:sqlite3

于 2008-10-02T15:51:57.257 回答
5

sqlite-manager , firefox 插件:允许您在 SQL 脚本中导出 SQLite 数据库。

数据库>导出数据库>导出到文件

(更正 firefox 35 bugg 必须更正扩展代码,如以下网页所示: How to fix your optional sqlite manager module to work

命令行

sqlite3 DB_name .dump > DB_name.sql

在 SQL 脚本中导出 sqlite 数据库。

来自网址:http ://doc.ubuntu-fr.org/sqlite 。

于 2015-01-26T13:53:25.423 回答
0

一个想法是做这样的事情: - 在 sql lite 中查看 squema 并获取 CREATE TABLE 命令。- 在 SQL SERVER 中执行、解析 sql - 为每一行创建一个 INSERT 语句的旅行数据。(也解析sql)

这段代码是测试版,因为没有检测类型数据,也没有使用@parameter和命令对象,而是运行。

(您需要插入参考并安装 System.Data.SQLite;)

c#: 在 head cs 中插入此代码(或 neccesari)

使用系统;

使用 System.Collections.Generic;

使用 System.Text;

使用 System.Data;

使用 System.Data.SqlClient;

使用 System.Data.SQLite;

使用 System.Threading;

使用 System.Text.RegularExpressions;

使用 System.IO;

使用 log4net;

使用 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
    }
于 2016-11-07T11:10:23.550 回答
0

对于安卓。

adb root
adb shell
cd /data/com.xxx.package/databases/
sqlite3 db_name .dump >dump.sql
于 2019-06-19T07:53:17.537 回答