我正在开发一个使用.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.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);
}
}
}
我收到错误:
SQLite.SQLiteException:无法打开数据库文件
答案 0 :(得分:0)
您可以查看名为“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);
}
获取物品:
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;
}
通过这些示例,可以很容易地以相同的方式将其实现到您的应用程序中