简体   繁体   English

从SQLite导出到SQL Server

[英]Exporting from SQLite to SQL Server

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

SQLite does have a .dump option to run at the command line. SQLite确实有一个.dump选项可以在命令行运行。 Though I prefer to use the SQLite Database Browser application for managing SQLite databases. 虽然我更喜欢使用SQLite数据库浏览器应用程序来管理SQLite数据库。 You can export the structure and contents to a .sql file that can be read by just about anything. 您可以将结构和内容导出到几乎可以读取的.sql文件中。 File > Export > Database to SQL file. 文件>导出>数据库到SQL文件。

I know that this is old thread, but I think that this solution should be also here. 我知道这是旧线程,但我认为这个解决方案也应该在这里。

  • Install ODBC driver for SQLite 为SQLite安装ODBC驱动程序
  • Run odbcad32 for x64 or C:\\Windows\\SysWOW64\\odbcad32.exe for x86 运行odbcad32 for x64或C:\\ Windows \\ SysWOW64 \\ odbcad32.exe for x86
  • Create SYSTEM DSN, where you select SQLite3 ODBC Driver 创建SYSTEM DSN,在其中选择SQLite3 ODBC Driver
  • Then you fill up form where Database Name is filepath to sqlite database 然后填写数据库名称为filepath的表单到sqlite数据库

Then in SQL Server run under sysadmin 然后在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

Then you can run your queries as normal user eg 然后您可以像普通用户一样运行查询

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

or you can use something like this for larger tables. 或者您可以使用像这样的较大的表。

The SQLite .dump command will output the entire contents of the database as an ASCII text file. SQLite .dump命令将输出数据库的全部内容作为ASCII文本文件。 This file is in standard SQL format, so it can be imported into any SQL database. 此文件采用标准SQL格式,因此可以导入任何SQL数据库。 More details on this page: sqlite3 有关此页面的更多详细信息: sqlite3

sqlite-manager , firefox add-on: allows you to export an SQLite database in a SQL script. sqlite-manager ,firefox附加组件:允许您在SQL脚本中导出SQLite数据库。

Data Base>Export Database>Export to file 数据库>导出数据库>导出到文件

(Correction firefox 35 bugg obliged to correct the extension code as indicate to the following web page: How to fix your optional sqlite manager module to work ) (更正firefox 35 bugg有义务更正扩展代码,如以下网页所示: 如何修复可选的sqlite manager模块工作

Command line : 命令行

sqlite3 DB_name .dump > DB_name.sql

exports the sqlite database in a SQL script. 在SQL脚本中导出sqlite数据库。

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

A idea is do some thing like this: - View squema in sql lite and get the CREATE TABLE command. 一个想法是做这样的事情: - 在sql lite中查看squema并获取CREATE TABLE命令。 - Execute, parsing sql, in SQL SERVER - Travel data creating a INSERT statment for each row. - 在SQL SERVER中执行,解析sql - 为每行创建INSERT语句的旅行数据。 (parsing sql too) (也解析sql)

This code is beta, because no detect type data, and no use @parameter and command object, but run. 这段代码是beta版,因为没有检测类型数据,也没有使用@parameter和command对象,而是运行。

(You need insert reference and install System.Data.SQLite;) (您需要插入引用并安装System.Data.SQLite;)

c#: Insert this code (or neccesari) in head cs c#:在head cs中插入此代码(或neccesari)

using System; 使用系统;

using System.Collections.Generic; 使用System.Collections.Generic;

using System.Text; 使用System.Text;

using System.Data; 使用System.Data;

using System.Data.SqlClient; 使用System.Data.SqlClient;

using System.Data.SQLite; 使用System.Data.SQLite;

using System.Threading; 使用System.Threading;

using System.Text.RegularExpressions; 使用System.Text.RegularExpressions;

using System.IO; 使用System.IO;

using log4net; 使用log4net;

using System.Net; 使用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
    }

For Android. 对于Android。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM