2011-02-08 10 views
17

ile bir sqlite örneğine ihtiyacınız var. Monodroid ile sqlite'ın kullanılmasının bir örneğini işaret eden var mı? Bir tane bile bulamadım.Monodroid

+0

Ben de bir blog yazısı yazdı Burada: http://www.elucidsoft.com/blog/2011/12/31/mono-android-working-with-sqlite/ – emalamisura

cevap

36

ApiDemo örneğine bir SQLite demosu eklemem gerekiyor.

bunun olmasına edeceğiz ne zaman bilmiyorum yana

, burada hızlı ve kirli sürümü:

Ancak, Mono.Data kullanmak Android 2.2 veya daha hedeflemesi gerekir aşağıdaki kodu kullanma. sQLite. Daha eski bir Android sürümünü hedeflemeniz gerekiyorsa, managed-sqlite gibi tamamen yönetilen bir yedeğe bakmalısınız. Ayrıca, bu örnek MonoDroid SDK'da bulunan Mono.Data.Sqlite.dll kullanıyor.

İlk önce, proje derleme referanslarınızı düzenleyin ve Mono.Data.Sqlite.dll ve System.Data.dll için bir başvuru ekleyin.

İkinci olarak, kaynak kodu içinde ekleyin:

using System.Data; 
using Mono.Data.Sqlite; 

Son olarak, kullanım ye Normal ADO.NET kodu: SQLite ile çalışma hakkında ipuçları gerekiyorsa

string dbPath = Path.Combine (
     Environment.GetFolderPath (Environment.SpecialFolder.Personal), 
     "items.db3"); 
bool exists = File.Exists (dbPath); 
if (!exists) 
    SqliteConnection.CreateFile (dbPath); 
var connection = new SqliteConnection ("Data Source=" + dbPath); 
connection.Open(); 
if (!exists) { 
    // This is the first time the app has run and/or that we need the DB. 
    // Copy a "template" DB from your assets, or programmatically create one. 
    var commands = new[]{ 
     "CREATE TABLE [Items] (Key ntext, Value ntext);", 
     "INSERT INTO [Items] ([Key], [Value]) VALUES ('sample', 'text')" 
    }; 
    foreach (var command in commands) { 
     using (var c = connection.CreateCommand()) { 
      c.CommandText = command; 
      c.ExecuteNonQuery(); 
     } 
    } 
} 
// use `connection`... 
// here, we'll just append the contents to a TextView 
using (var contents = connection.CreateCommand()) { 
    contents.CommandText = "SELECT [Key], [Value] from [Items]"; 
    var r = contents.ExecuteReader(); 
    while (r.Read()) 
     MyTextView.Text += string.Format ("\n\tKey={0}; Value={1}", 
       r ["Key"].ToString(), r ["Value"].ToString()); 
} 
connection.Close(); 
+1

Çok teşekkürler! Bu benim için eksik bağlantı. Teşekkürler – basheps

+1

+1. Bu benim için de eksik bağlantıydı. –

+0

System.IO, Mono'da android için mevcut değil. Android uygulamaları için Mono'yu nasıl kullanabiliriz? – dinesh