C# 使用Sqlite中的数据填充UITableViewSource
Im使用Xamarin和mono创建iOS应用程序。Im使用UITableView显示集合中可用的记录。我遇到的问题是将我用Sqlite数据库中的数据创建的列表放入DataSource对象以填充表。我知道Sqlite查询可以工作,但我无法填充表 这里是Xamarin构建的默认数据源C# 使用Sqlite中的数据填充UITableViewSource,c#,ios,uitableview,xamarin,xamarin.ios,C#,Ios,Uitableview,Xamarin,Xamarin.ios,Im使用Xamarin和mono创建iOS应用程序。Im使用UITableView显示集合中可用的记录。我遇到的问题是将我用Sqlite数据库中的数据创建的列表放入DataSource对象以填充表。我知道Sqlite查询可以工作,但我无法填充表 这里是Xamarin构建的默认数据源 using System; using System.Collections.Generic; using Foundation; using UIKit; namespace RecordStore {
using System;
using System.Collections.Generic;
using Foundation;
using UIKit;
namespace RecordStore
{
public class DataSource : UITableViewSource
{
static readonly NSString CellIdentifier = new NSString ("Cell");
readonly List<object> objects = new List<object> ();
readonly MasterViewController controller;
public DataSource (MasterViewController controller)
{
this.controller = controller;
}
public IList<object> Objects {
get { return objects; }
}
// Customize the number of sections in the table view.
public override nint NumberOfSections (UITableView tableView)
{
return 1;
}
public override nint RowsInSection (UITableView tableview, nint section)
{
return objects.Count;
}
// Customize the appearance of table view cells.
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var cell = tableView.DequeueReusableCell (CellIdentifier, indexPath);
cell.TextLabel.Text = objects [indexPath.Row].ToString ();
return cell;
}
public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
{
// Return false if you do not want the specified item to be editable.
return true;
}
public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
if (editingStyle == UITableViewCellEditingStyle.Delete) {
// Delete the row from the data source.
objects.RemoveAt (indexPath.Row);
controller.TableView.DeleteRows (new [] { indexPath }, UITableViewRowAnimation.Fade);
} else if (editingStyle == UITableViewCellEditingStyle.Insert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
}
}
以下是数据库代码:
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using Mono.Data.Sqlite;
namespace RecordStore
{
public class Database
{
public Database ()
{
}
public List<string> GetItems() {
List<string> list = new List<string> ();
var connection = GetConnection ();
using (var cmd = connection.CreateCommand ()) {
connection.Open ();
cmd.CommandText = "SELECT `Album` FROM Records";
using (var reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
for (int i = 0; i < reader.FieldCount; ++i) {
list.Add (reader[i].ToString());
}
}
}
connection.Close ();
}
return list;
}
static SqliteConnection GetConnection()
{
var documents = Environment.GetFolderPath (
Environment.SpecialFolder.Personal);
string db = Path.Combine (documents, "mydb.db3");
bool exists = File.Exists (db);
if (!exists)
SqliteConnection.CreateFile (db);
var conn = new SqliteConnection("Data Source=" + db);
if (!exists) {
var commands = new[] {
"CREATE TABLE Records (ID INTEGER PRIMARY KEY AUTOINCREMENT, Artist ntext, Album ntext, Genre ntext, Year ntext)",
// @TODO: Remove at production
// WARNING: never insert user-entered data with embedded parameter values
"INSERT INTO Records (ID, Artist, Album, Genre, Year) VALUES (null, 'A Day to Remember', 'Homesick', 'Post-Hardcore', '2008')"
};
conn.Open ();
foreach (var cmd in commands) {
using (var c = conn.CreateCommand()) {
c.CommandText = cmd;
c.CommandType = CommandType.Text;
c.ExecuteNonQuery ();
}
}
conn.Close ();
}
return conn;
}
static void Write(SqliteDataReader reader, int index)
{
Console.Error.Write("({0} '{1}')", reader.GetName(index), reader[index]);
}
}
}
使用系统;
使用System.IO;
使用系统数据;
使用System.Collections.Generic;
使用Mono.Data.Sqlite;
命名空间记录存储
{
公共类数据库
{
公共数据库()
{
}
公共列表GetItems(){
列表=新列表();
var connection=GetConnection();
使用(var cmd=connection.CreateCommand()){
connection.Open();
cmd.CommandText=“从记录中选择“相册”;
使用(var reader=cmd.ExecuteReader()){
while(reader.Read()){
对于(int i=0;i
任何帮助都将不胜感激,遗憾的是我一点也不明白。从数据库获取数据的代码在哪里?为什么不直接从DataSource构造函数调用GetItems()并将其分配给对象呢?实际上,我只是能够让它工作,但该项目在我的iPhone上不工作。说SQLITE3DLL不可用时崩溃
using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using Mono.Data.Sqlite;
namespace RecordStore
{
public class Database
{
public Database ()
{
}
public List<string> GetItems() {
List<string> list = new List<string> ();
var connection = GetConnection ();
using (var cmd = connection.CreateCommand ()) {
connection.Open ();
cmd.CommandText = "SELECT `Album` FROM Records";
using (var reader = cmd.ExecuteReader ()) {
while (reader.Read ()) {
for (int i = 0; i < reader.FieldCount; ++i) {
list.Add (reader[i].ToString());
}
}
}
connection.Close ();
}
return list;
}
static SqliteConnection GetConnection()
{
var documents = Environment.GetFolderPath (
Environment.SpecialFolder.Personal);
string db = Path.Combine (documents, "mydb.db3");
bool exists = File.Exists (db);
if (!exists)
SqliteConnection.CreateFile (db);
var conn = new SqliteConnection("Data Source=" + db);
if (!exists) {
var commands = new[] {
"CREATE TABLE Records (ID INTEGER PRIMARY KEY AUTOINCREMENT, Artist ntext, Album ntext, Genre ntext, Year ntext)",
// @TODO: Remove at production
// WARNING: never insert user-entered data with embedded parameter values
"INSERT INTO Records (ID, Artist, Album, Genre, Year) VALUES (null, 'A Day to Remember', 'Homesick', 'Post-Hardcore', '2008')"
};
conn.Open ();
foreach (var cmd in commands) {
using (var c = conn.CreateCommand()) {
c.CommandText = cmd;
c.CommandType = CommandType.Text;
c.ExecuteNonQuery ();
}
}
conn.Close ();
}
return conn;
}
static void Write(SqliteDataReader reader, int index)
{
Console.Error.Write("({0} '{1}')", reader.GetName(index), reader[index]);
}
}
}