【问题标题】:Exporting from SQLite to SQL Server从 SQLite 导出到 SQL Server
【发布时间】:2010-09-14 20:28:31
【问题描述】:

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

【问题讨论】:

    标签: sql-server database sqlite data-migration


    【解决方案1】:

    适用于 Android。

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

    【讨论】:

      【解决方案2】:

      一个想法是做这样的事情: - 在 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
          }
      

      【讨论】:

        【解决方案3】:

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

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

        或者您可以将this 之类的东西用于更大的表格。

        【讨论】:

        • SELECT * INTO SQLServerDATA FROM openquery(OldSQLite, 'select * from SQLiteData') ------- 你可以安装 64 位驱动位并为 64 位创建 ODBC(system32 文件夹中的 odbcad32 - SQLite3 ODBC 驱动程序)
        【解决方案4】:

        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

        【讨论】:

        • 插件现在似乎已经死了,firefox 57.0 版显示它不兼容且无法使用...
        【解决方案5】:

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

        【讨论】:

        • 好吧 .dump 命令本身会输出到屏幕上。您还需要使用 .output 命令将其放入文件中。
        【解决方案6】:

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

        【讨论】:

        • 导出的文件确实需要针对语法差异进行一些调整,但不要太复杂。谢谢
        • 从技术上讲,SQLite shell 不是 SQLite,而是一个链接到 SQLite 的控制台应用程序,它只是一个库。
        • 这为我指明了正确的方向,但我正在寻找语法,所以我将它包含在此处:sqlite3 my_db.db -batch ".dump" &gt; my_db.sql
        • 在我的情况下它很复杂......一个字段包含日期和时间,并且在导出时使用毫秒导出,当我尝试在 SQL Server 中执行它时这是不兼容的:-(跨度>
        • 它创建的 sql 文件有很多错误,我要为此提交一个新问题还是我做错了什么?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-04-22
        • 2014-04-30
        • 1970-01-01
        • 2014-11-21
        • 2011-01-02
        • 1970-01-01
        相关资源
        最近更新 更多