C# SQLite-net中的名称表
我正在构建一个windows8c#/XAML应用程序,它使用SQLite作为存储数据库,并尝试使用SQLite-net语法创建多个表 根据我到目前为止的研究,表是基于类创建的。首先,我通过以下方式创建了一个“Account”类:C# SQLite-net中的名称表,c#,sqlite,windows-8,windows-runtime,sqlite-net,C#,Sqlite,Windows 8,Windows Runtime,Sqlite Net,我正在构建一个windows8c#/XAML应用程序,它使用SQLite作为存储数据库,并尝试使用SQLite-net语法创建多个表 根据我到目前为止的研究,表是基于类创建的。首先,我通过以下方式创建了一个“Account”类: public class Account { [PrimaryKey, AutoIncrement] public int ID { get; set; } public string Name { get; set; } public
public class Account
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set;}
}
然后创建一个表,然后通过以下方式在代码中输入初始数据:
private static readonly string _dbPath =
Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "data.sqlite");
using (var db = new SQLite.SQLiteConnection(_dbPath))
{
db.CreateTable<Account>();
db.RunInTransaction(() =>
db.Insert(new Account()
{
Name = "MyCheckingAccount",
Type = "Checking",
})
);
}
私有静态只读字符串\u dbPath=
Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path,“data.sqlite”);
使用(var db=new SQLite.SQLiteConnection(_dbPath))
{
db.CreateTable();
db.RunInTransaction(()=>
db.插入(新帐户()
{
Name=“MyCheckingAccount”,
Type=“检查”,
})
);
}
我想创建多个帐户表,但是db.CreateTable()
语法只创建了一个表,数据被插入到带有db.Insert()的列中。
我不知道在哪里输入表本身的名称
如何创建多个表,即一个名为“BusinessAccounts”,另一个基于Account类的“PersonalAccounts”
有没有一种方法可以通过SQLite net实现这一点?或者我是否需要以某种方式显式地写出SQLite命令?SQLite Net使用类名创建表以及更新数据。要执行您想要的操作,您需要创建单独的类。避免重复公共字段的一种方法是使用继承:
public class Account
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set;}
}
public class BusinessAccounts : Account { }
public class PersonalAccounts : Account { }
要创建表,请执行以下操作:
db.CreateTable<BusinessAccounts>();
db.CreateTable<PersonalAccounts>();
请注意,上面的代码未经测试。您需要确保正确创建表(例如,使用正确的主键和自动增量字段)。Sqlite Net使用类名创建表,并更新数据。要执行您想要的操作,您需要创建单独的类。避免重复公共字段的一种方法是使用继承:
public class Account
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public string Type { get; set;}
}
public class BusinessAccounts : Account { }
public class PersonalAccounts : Account { }
要创建表,请执行以下操作:
db.CreateTable<BusinessAccounts>();
db.CreateTable<PersonalAccounts>();
请注意,上面的代码未经测试。您需要确保正确创建表(例如,使用正确的主键和自动增量字段)。这个答案似乎过时了,在SQLite net中,您现在可以使用类上的属性来覆盖表名,例如:
[SQLite.Table("table_customers")]
public class Customer
{
[MaxLength(3)]
public string code { get; set; }
[MaxLength(255)]
public string name { get; set; }
}
因此,它将创建/更新该表。这个答案似乎过时了,在SQLite net中,您现在可以使用类上的属性来覆盖表名,例如:
[SQLite.Table("table_customers")]
public class Customer
{
[MaxLength(3)]
public string code { get; set; }
[MaxLength(255)]
public string name { get; set; }
}
因此它将创建/更新该表。好的,谢谢。如何处理用户输入?i、 e.每次用户创建帐户时,都会使用用户给定的名称创建一个表。这是可能的吗?@smillerc-您试图做的并不适合Sqlite Net。如上所述,Sqlite Net使用类名作为表名和数据更新。一个可能的替代方案是创建一个Accounts表,该表有一个字段来指示帐户类型。是否还有其他好的替代方案可以通过app store的认证?我只见过SQLite net的例子…@smillerc-我没有任何SQLite net替代品的经验,所以我真的不能推荐任何。谷歌可能是你最好的选择。好的,谢谢。如何处理用户输入?i、 e.每次用户创建帐户时,都会使用用户给定的名称创建一个表。这是可能的吗?@smillerc-您试图做的并不适合Sqlite Net。如上所述,Sqlite Net使用类名作为表名和数据更新。一个可能的替代方案是创建一个Accounts表,该表有一个字段来指示帐户类型。是否还有其他好的替代方案可以通过app store的认证?我只见过SQLite net的例子…@smillerc-我没有任何SQLite net替代品的经验,所以我真的不能推荐任何。谷歌可能是你最好的选择。