Hi guys
I'm gonna use my existing database in the project but I get an error
I used "https://www.nuget.org/packages/sqlite-net-pcl" in my project
this DB class :
- public static SQLiteConnection sqliteCon()
- {
- string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
- string dbPath = Path.Combine(path, "fitnessDBupdate.db");
- CopyDatabaseIfNotExists(dbPath);
- using (var conn = new SQLiteConnection(dbPath, true))
- {
- return conn;
- }
- }
-
- public bool createMembers()
- {
- try
- {
- using (var conn = sqliteCon())
- {
- conn.CreateTable<Members>();
- }
- return true;
- }
- catch (SQLiteException ex)
- {
- Log.Info("SQLite EX", ex.ToString());
- return false;
- }
- }
-
- private static void CopyDatabaseIfNotExists(string dbPath)
- {
- try
- {
- string dbName = "fitnessDBupdate.db";
- dbPath = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), dbName);
-
- if (!File.Exists(dbPath))
- {
- using (BinaryReader br = new BinaryReader(Application.Context.Assets.Open(dbName)))
- {
- using (BinaryWriter bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
- {
- byte[] buffer = new byte[2048];
- int len = 0;
- while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
- {
- bw.Write(buffer, 0, len);
- }
- }
- }
- }
- }
- catch (SQLiteException ex)
- {
- Log.Info("SQLite Ex", ex.ToString());
- }
- }
and in login activity :
- using Android.App;
- using Android.OS;
- using Android.Widget;
- using ExsitingSQLite.Resources.DataHelper;
- using System;
- using System.Linq;
-
- namespace ExsitingSQLite
- {
- [Activity(Label = "ExsitingSQLite", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- Button btnRegister;
- Button btnLog;
- EditText edtName;
- EditText edtPass;
-
- DataBase db;
-
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- SetContentView(Resource.Layout.Main);
-
- btnRegister = FindViewById<Button>(Resource.Id.btnRegisterPage);
- btnLog = FindViewById<Button>(Resource.Id.btnLogin);
- edtName = FindViewById<EditText>(Resource.Id.edtName);
- edtPass = FindViewById<EditText>(Resource.Id.edtPass);
-
- btnRegister.Click += btnRegister_Click;
- btnLog.Click += btnLog_Click;
- }
-
- private void btnLog_Click(object sender, EventArgs e)
- {
- db = new DataBase();
- var data = db.listMembers();
- var data2 = data.Where(x => x.fullName == edtName.Text && x.password == edtPass.Text).FirstOrDefault();
- if (data2 != null)
- {
- StartActivity(typeof(HomeActivity));
- }
- else
- {
- Toast.MakeText(this, "??? ?????? ?? ???? ???? ?? ????? ???", ToastLength.Short).Show();
- }
- }
-
- private void btnRegister_Click(object sender, EventArgs e)
- {
- StartActivity(typeof(RegisterActivity));
- }
- }
- }
thanks