我想创建一个多个SQLite表,但是我不知道如何进行。
我的项目流程:
-当我单击登录按钮时,如果填写了用户名和密码,它应该创建SQLite数据库和表。
需要创建表:
用户表(字段:UserID(字符串),UsrPassword(字符串),ContactId(int),Status(字符串))
零售商(字段:Retailer_Name(字符串),Retailer_Handler(整数))
我已经完成的工作:
1.我已经添加了 SQLite Nuget程序包
2.我在接口中添加了 SQLiteAsyncConnection GetConnection();
3.我为每个项目(Android和UWP)添加了数据库创建器
4.我已经将我的 LoginPage 表单绑定到我的 ViewModel
我的代码如下:
ISQLiteDB.cs
using SQLite;
using System;
using System.Collections.Generic;
using System.Text;
namespace TBSMobileApplication.Data
{
public interface ISQLiteDB
{
SQLiteAsyncConnection GetConnection();
}
}
LoginPage.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TBSMobileApplication.View.LoginPage"
BackgroundColor="#ecf0f1">
<ContentPage.Content>
<StackLayout
VerticalOptions="StartAndExpand">
<StackLayout.Padding>
<OnPlatform
x:TypeArguments="Thickness"
iOS="20"
Android="20,100,20,0">
</OnPlatform>
</StackLayout.Padding>
<Label
Text="Username"
TextColor="#34495e"
Font="Arial,10"/>
<Entry
Placeholder="Username"
PlaceholderColor="#95a5a6"
FontSize="12"
FontFamily="Arial"
x:Name="entUsername"
Text="{Binding Username}"/>
<Label
Text="Password"
TextColor="#34495e"
Font="Arial,10"/>
<Entry
Placeholder="Password"
PlaceholderColor="#95a5a6"
FontSize="12"
FontFamily="Arial"
IsPassword="True"
x:Name="entPassword"
Text="{Binding Password}"/>
<Button
Text="Login"
FontSize="12"
HorizontalOptions="Start"
BackgroundColor="#3498db"
Command="{Binding LoginCommand}"/>
</StackLayout>
</ContentPage.Content>
</ContentPage>
LoginPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TBSMobileApplication.ViewModel;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TBSMobileApplication.View
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class LoginPage : ContentPage
{
public LoginPage ()
{
InitializeComponent ();
BindingContext = new LoginPageViewModel();
MessagingCenter.Subscribe<LoginPageViewModel,string>(this, "Login Alert",(sender,Username) =>
{
DisplayAlert("Login Alert", "Please fill-up the form", "Ok");
});
}
}
}
LoginPageViewModel.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Input;
using TBSMobileApplication.View;
using Xamarin.Forms;
namespace TBSMobileApplication.ViewModel
{
public class LoginPageViewModel : INotifyPropertyChanged
{
void OnProperyChanged(string PropertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
}
public string username;
public string password;
public string Username
{
get { return username; }
set
{
username = value;
OnProperyChanged(nameof(Username));
}
}
public string Password
{
get { return password; }
set
{
password = value;
OnProperyChanged(nameof(Password));
}
}
public ICommand LoginCommand { get; set; }
public LoginPageViewModel()
{
LoginCommand = new Command(OnLogin);
}
public void OnLogin()
{
if (string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password))
{
MessagingCenter.Send(this, "Login Alert", Username);
}
else
{
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
}
AndroidSQLiteDB.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using SQLite;
using TBSMobileApplication.Data;
using TBSMobileApplication.Droid.Data;
using Xamarin.Forms;
[assembly: Dependency(typeof(AndroidSQLiteDb))]
namespace TBSMobileApplication.Droid.Data
{
public class AndroidSQLiteDb : ISQLiteDB
{
public SQLiteAsyncConnection GetConnection()
{
var dbFileName = "backend.db3";
var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
var path = Path.Combine(documentsPath, dbFileName);
return new SQLiteAsyncConnection(path);
}
}
}
WindowsSQLiteDB.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SQLite;
using TBSMobileApplication.Data;
using TBSMobileApplication.UWP.Data;
using Windows.Storage;
using Xamarin.Forms;
[assembly: Dependency(typeof(WindowsSQLiteDb))]
namespace TBSMobileApplication.UWP.Data
{
public class WindowsSQLiteDb : ISQLiteDB
{
public SQLiteAsyncConnection GetConnection()
{
var dbFileName = "backend.db3";
var documentsPath = ApplicationData.Current.LocalFolder.Path;
var path = Path.Combine(documentsPath, dbFileName);
return new SQLiteAsyncConnection(path);
}
}
}
答案 0 :(得分:1)
您不想在用户单击按钮时创建表-应该在您的应用程序首次在给定设备上启动时真正创建数据库和表。但是过程是一样的
// get the connection
var db = DependencyService.Get< ISQLiteDB>();
var conn = db.GetConnection();
// create the tables
if (conn != null) {
await conn.CreateTableAsync<Users>();
await conn.CreateTableAsync<Retailer>();
}
每个数据库模型都需要一个类
public class Retailer {
public string Retailer_Name { get; set; }
public int Retailer_Handler { get; set; }
}