C# 如何在Visual Studio/Xamarin项目中打开.sqlitedb文件?
我正在从事一个项目,该项目使用.sqlitedb格式在android手机上以脱机模式显示地图。我的文件类型与SO上的类似问题不相关,因此我无法打开与有相同问题但文件类型相同的人当前编写的文件。s3db,我做错了什么C# 如何在Visual Studio/Xamarin项目中打开.sqlitedb文件?,c#,android-sqlite,C#,Android Sqlite,我正在从事一个项目,该项目使用.sqlitedb格式在android手机上以脱机模式显示地图。我的文件类型与SO上的类似问题不相关,因此我无法打开与有相同问题但文件类型相同的人当前编写的文件。s3db,我做错了什么 using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using SQLite; using SQLitePCL;
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using SQLite;
using SQLitePCL;
using System.Data;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using System.Drawing;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.ComponentModel;
namespace MapplicationIII
{
public class TilesProvider : DataModel
{
public SQLiteConnection db_con;
public void Prepare() { }
public sqlite3_stmt stmt;
public TilesProvider(String dbPath)
{
db_con = new SQLiteConnection(@"Data Source = [file path]\MapDataBase.sqlitedb;");
//DataTable tables = db_con.GetTableInfo<db_con.GetTableInfo(string tiles_tb);
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
使用SQLite;
使用SQLitePCL;
使用系统数据;
使用Android.App;
使用Android.Content;
使用Android.OS;
使用Android.Runtime;
使用Android.Views;
使用Android.Widget;
使用Android.Graphics;
使用系统图;
使用System.Runtime.Serialization.Formatters.Binary;
使用System.IO;
使用系统组件模型;
命名空间映射应用II
{
公共类TileProvider:DataModel
{
公共连接数据库;
public void Prepare(){}
公共sqlite3_stmt stmt;
公共tileProvider(字符串dbPath)
{
db_con=new SQLiteConnection(@“数据源=[文件路径]\MapDataBase.sqlitedb;”);
//DataTable tables=db_con.GetTableInfo您可以查看名为“Tasky”的Xamarin示例项目
要创建数据库,请执行以下操作:
public SqliteConnection connection;
public string path;
static object locker = new object ();
/// <summary>
/// Initializes a new instance of the <see cref="Tasky.DL.TaskDatabase"/> TaskDatabase.
/// if the database doesn't exist, it will create the database and all the tables.
/// </summary>
public TaskDatabase (string dbPath)
{
var output = "";
path = dbPath;
// create the tables
bool exists = File.Exists (dbPath);
if (!exists) {
connection = new SqliteConnection ("Data Source=" + dbPath);
connection.Open ();
var commands = new[] {
"CREATE TABLE [Items] (_id INTEGER PRIMARY KEY ASC, Name NTEXT, Notes NTEXT, Done INTEGER);"
};
foreach (var command in commands) {
using (var c = connection.CreateCommand ()) {
c.CommandText = command;
var i = c.ExecuteNonQuery ();
}
}
} else {
// already exists, do nothing.
}
Console.WriteLine (output);
}
公共SqliteConnection;
公共字符串路径;
静态对象锁定器=新对象();
///
///初始化TaskDatabase的新实例。
///如果数据库不存在,它将创建数据库和所有表。
///
公共任务数据库(字符串dbPath)
{
var输出=”;
path=dbPath;
//创建表
bool exists=File.exists(dbPath);
如果(!存在){
连接=新的SqliteConnection(“数据源=“+dbPath”);
connection.Open();
var命令=new[]{
创建表[项目](\u id INTEGER主键ASC,Name NTEXT,Notes NTEXT,Done INTEGER)
};
foreach(命令中的var命令){
使用(var c=connection.CreateCommand()){
c、 CommandText=命令;
var i=c.ExecuteNonQuery();
}
}
}否则{
//已经存在,什么也不做。
}
Console.WriteLine(输出);
}
要获取项目,请执行以下操作:
public IEnumerable<Task> GetItems ()
{
var tl = new List<Task> ();
lock (locker) {
connection = new SqliteConnection ("Data Source=" + path);
connection.Open ();
using (var contents = connection.CreateCommand ()) {
contents.CommandText = "SELECT [_id], [Name], [Notes], [Done] from [Items]";
var r = contents.ExecuteReader ();
while (r.Read ()) {
tl.Add (FromReader(r));
}
}
connection.Close ();
}
return tl;
}
public IEnumerable GetItems()
{
var tl=新列表();
锁(储物柜){
连接=新的SqliteConnection(“数据源=“+路径”);
connection.Open();
使用(var contents=connection.CreateCommand()){
contents.CommandText=“从[项目]中选择[_id]、[名称]、[注释]、[完成]”;
var r=contents.ExecuteReader();
while(r.Read()){
tl.添加(FromReader(r));
}
}
connection.Close();
}
返回tl;
}
有了这个例子,应该很容易以同样的方式将其实现到您的应用程序中您可以提供项目的链接吗?我已经有了一个数据库。我需要的是代码,以便能够打开.sqlitedb文件格式。在这种情况下?@E.a.O.S那么您只需要底部的代码。您不能复制和粘贴它。您需要了解数据库的结构,并创建一个命令文本来选择所有重要信息。如果您向我发送数据库,我可以帮助您,但我有这些部分要做,只是错误在于我的文件路径不被接受,但我可以将数据库发送给您,我将它发送到哪里?