【问题标题】:Reading SQLite Database as File in C#在 C# 中将 SQLite 数据库作为文件读取
【发布时间】:2012-12-05 08:02:15
【问题描述】:

对于如何读取来自我的 NexusOne 的 SQLite 数据库并使用 C#.Net 将其读取到我的 PC 上,我的脑海中出现了一些问题。

在我的 android 应用程序的初始加载中,数据库将作为 OutputStream 从 Asset 复制到 /data/data/com.example.myapp/databases。这是代码。

        //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0) {
        myOutput.write(buffer, 0, length);
    }

    //Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

我创建了一个 C# 程序来读取 SQLite 数据库,但是在使用 System.Data.SQLite 初始化连接时,它总是抛出一个错误“没有这样的表错误”。

当我在SQLiteConnection connection = new SQLiteConnection("Data Source=MyDB.db") 中放置断点时,connection.DataBase 始终等于“ma​​in”。数据库名称应该是“MyDB”而不是“main”。似乎我的 sqlite 数据库文件无法读取,可能是因为它是使用 Stream 创建的,或者可能是因为它不是数据库文件?因为当我检查文件类型时,它显示的是“文件”而不是“数据库文件”。

任何人都知道如何使用 C# 读取 sqlite 数据库文件。我首先想到的是使用FileStreamStreamReader/StreamWriter。但我不知道如何开始。

【问题讨论】:

    标签: c# android sqlite filestream outputstream


    【解决方案1】:

    使用SQLite Admin 之类的工具来确定数据库中表的名称或是否可以打开。然后,一旦您获得了这些信息,您应该能够使用 SQLiteConnection 对象打开数据库。

    【讨论】:

    • 感谢您的回复。我正在使用 SQLite 数据库浏览器浏览我的数据,并且我知道该表就在那里。
    • @klaydze 在某个网站上把你的桌子扔了,我去看看。
    猜你喜欢
    • 2019-09-26
    • 2021-12-17
    • 1970-01-01
    • 2015-05-06
    • 1970-01-01
    • 2021-01-09
    • 2010-11-04
    • 2017-04-11
    • 2023-02-01
    相关资源
    最近更新 更多