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

Kru*_*nal 3 iphone monodevelop xamarin.ios ipad xcode4.2

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

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

She*_*age 8

我尝试过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...
}
Run Code Online (Sandbox Code Playgroud)

希望有所帮助.


dal*_*oto 5

要访问sqlite,你有很多选择,比如

  1. http://code.google.com/p/sqlite-net/非常有用,也是我首选的方法
  2. System.Data和Mono.Data.SQLite http://docs.xamarin.com/ios/advanced_topics/system.data
  3. Mono的EntityFramework http://www.taimila.com/entify/index.php(不知道它是否还在开发中)
  4. Vici Cool Storage这是一篇很好的博客文章http://blog.activa.be/index.php/2010/02/vici-coolstorage-orm-on-monotouch-made-simple/和Vici的页面http:// viciproject.com/wiki/projects/coolstorage/home

还有很多其他人甚至Apple的CoreData都看到了(http://www.sgmunn.com/?p=1),但我猜任何上述选项都比较简单.

希望这有助于您入门