iphone - 如何在 iPhone 的 monotouch 中创建 SQLite 数据库?

标签 iphone ipad xamarin.ios xcode4.2 monodevelop

我是 iPad 开发新手。我已经使用 Xcode 4 在 Objective C 中创建了两到三个 iPad 应用程序。现在我想使用 C# 在 MonoDevelop 工具中创建 iPad 应用程序。

我想创建一个基本的数据库项目,为此我需要一个使用 SQLite 数据库的基本教程。我在 Google 上搜索过,但没有找到任何教程。

最佳答案

我已经尝试过 SqLiteClient 和 SqLite-net。我建议您阅读此处的教程: sqlite-net

对我来说,我只需将 Sqlite.cs 文件放入我的项目中并使用它进行编译即可。

然后,使用您选择的工具创建一个 SQLite 数据库并将其添加到您的 MonoTouch 项目中,并确保将构建标志设置为“内容”。一旦您的项目中有 SQLite 数据库(将文件放在您喜欢的任何位置),您就可以以只读方式访问它...除非您执行类似于以下示例中的代码的操作...这会将文件重新定位到用户的个人文件夹,您的应用(和您的用户)将对其具有写入权限。

如果您需要用户在运行时对数据库执行 CRUD 操作,则需要在代码中包含类似以下内容以将文件复制到用户的个人文件夹:

using System.Linq;
using SQLite;

string personalFolder = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string dbName = "myDatabase.db";
string dbPath = Path.Combine ( personalFolder, dbName);

// in this example I will always delete and re-copy the db to the users personal folder...
// modify to suit your needs...
if (File.Exists (dbPath) {
    File.Delete(dbPath);
}
File.Copy (dbName, dbPath);
// note: myDatabase.db for this example is in the root project folder.

using (var db = new SQLiteConnection(dbPath)) {
    // query using Linq...
}

希望有帮助。

关于iphone - 如何在 iPhone 的 monotouch 中创建 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9323380/

相关文章:

ios - 核心数据中的主键位置

iphone - iPhone 上的 98kHz 音频文件

ios - UITableView 中的 NSRangeException

iphone - 以嵌入式 html 播放视频时,如何使用证书不正确的 https 连接播放视频 URL

c# - Expression.Compile 在 Monotouch 上做了什么?

iphone - 参数列表语法

iphone - AVPlayer 视频 SeekToTime

iphone - iOS 开发 : How can I induce low memory warnings on device?

xamarin.ios - MonoTouch 和 Mono for Android 中的 Entity Framework 支持

c# - 如何在没有文本框的情况下获取键盘事件?