Skip to content

Commit

Permalink
Sqlite in Xamarin.Android
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangGaoxing committed Jan 27, 2018
1 parent 15e06e1 commit a96dc8a
Show file tree
Hide file tree
Showing 19 changed files with 770 additions and 0 deletions.
27 changes: 27 additions & 0 deletions SqliteDemo/SqliteDemo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2026
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteDemo", "SqliteDemo\SqliteDemo.csproj", "{06A73000-DB5D-42F9-BA5F-A1978003AA4E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.Build.0 = Release|Any CPU
{06A73000-DB5D-42F9-BA5F-A1978003AA4E}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CEF344A6-F7AB-4726-8587-FC793454C218}
EndGlobalSection
EndGlobal
43 changes: 43 additions & 0 deletions SqliteDemo/SqliteDemo/Acitvities/AddActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
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 SqliteDemo.Managers;
using SqliteDemo.Models;

namespace SqliteDemo.Acitvities
{
[Activity(Label = "AddActivity")]
public class AddActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.AddLayout);

Button add = FindViewById<Button>(Resource.Id.add_btn_add);
EditText name = FindViewById<EditText>(Resource.Id.add_edit_name);
EditText sex = FindViewById<EditText>(Resource.Id.add_edit_sex);
EditText age = FindViewById<EditText>(Resource.Id.add_edit_age);

add.Click += (s, e) =>
{
SqliteManager<Person> sqlite = new SqliteManager<Person>();

sqlite.Insert(new Person
{
Name = name.Text,
Sex = sex.Text,
Age = Convert.ToInt32(age.Text)
});
};
}
}
}
36 changes: 36 additions & 0 deletions SqliteDemo/SqliteDemo/Acitvities/DeleteActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
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 SqliteDemo.Managers;
using SqliteDemo.Models;

namespace SqliteDemo.Acitvities
{
[Activity(Label = "DeleteActivity")]
public class DeleteActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.DeleteLayout);

Button del = FindViewById<Button>(Resource.Id.del_btn_del);
EditText id = FindViewById<EditText>(Resource.Id.del_edit_id);

del.Click += (s, e) =>
{
SqliteManager<Person> sqlite = new SqliteManager<Person>();

sqlite.DbConnection.Execute($"delete from Person where id = '{id.Text}'");
};
}
}
}
35 changes: 35 additions & 0 deletions SqliteDemo/SqliteDemo/Acitvities/QueryActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
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 SqliteDemo.Managers;
using SqliteDemo.Models;

namespace SqliteDemo.Acitvities
{
[Activity(Label = "QueryActivity")]
public class QueryActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.QueryLayout);

TextView res = FindViewById<TextView>(Resource.Id.que_text_res);

SqliteManager<Person> sqlite = new SqliteManager<Person>();
var list = sqlite.QueryAll();
foreach (var item in list)
{
res.Text += $"ID:{item.ID} Name:{item.Name} Sex:{item.Sex} Age:{item.Age}\n";
}
}
}
}
19 changes: 19 additions & 0 deletions SqliteDemo/SqliteDemo/Assets/AboutAssets.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Any raw assets you want to be deployed with your application can be placed in
this directory (and child directories) and given a Build Action of "AndroidAsset".

These files will be deployed with you package and will be accessible using Android's
AssetManager, like this:

public class ReadAsset : Activity
{
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);

InputStream input = Assets.Open ("my_asset.txt");
}
}

Additionally, some Android functions will automatically load asset files:

Typeface tf = Typeface.CreateFromAsset (Context.Assets, "fonts/samplefont.ttf");
44 changes: 44 additions & 0 deletions SqliteDemo/SqliteDemo/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using SqliteDemo.Acitvities;
using SqliteDemo.Managers;
using SqliteDemo.Models;

namespace SqliteDemo
{
[Activity(Label = "SqliteDemo", MainLauncher = true, Theme = "@android:style/Theme.Material.Light")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);

Button add = FindViewById<Button>(Resource.Id.main_btn_add);
Button del = FindViewById<Button>(Resource.Id.main_btn_del);
Button que = FindViewById<Button>(Resource.Id.main_btn_que);

add.Click += (s, e) =>
{
Intent dialog = new Intent(this, typeof(AddActivity));
StartActivity(dialog);
};
del.Click += (s, e) =>
{
Intent dialog = new Intent(this, typeof(DeleteActivity));
StartActivity(dialog);
};
que.Click += (s, e) =>
{
Intent dialog = new Intent(this, typeof(QueryActivity));
StartActivity(dialog);
};

SqliteManager<Person> sqlite = new SqliteManager<Person>();
sqlite.CreateTable();
}
}
}

130 changes: 130 additions & 0 deletions SqliteDemo/SqliteDemo/Managers/SqliteManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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.Net;
using SQLite.Net.Platform.XamarinAndroid;

namespace SqliteDemo.Managers
{
class SqliteManager<T> where T : class
{
/// <summary>
/// 数据库文件路径
/// </summary>
private string dbPath = string.Empty;

public string DbPath
{
get
{
if (string.IsNullOrEmpty(dbPath))
{
dbPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "info.db3");
}

return dbPath;
}
}

/// <summary>
/// 数据库连接
/// </summary>
public SQLiteConnection DbConnection
{
get
{
return new SQLiteConnection(new SQLitePlatformAndroid() ,DbPath);
}
}

public SqliteManager()
{

}

public SqliteManager(string dbPath)
{
this.dbPath = dbPath;
}

/// <summary>
/// 创建表
/// </summary>
public void CreateTable()
{
using (var db = DbConnection)
{
var c = db.CreateTable<T>();
}
}

/// <summary>
/// 插入数据
/// </summary>
/// <param name="item">插入值</param>
public void Insert(T item)
{
using (var db = DbConnection)
{
db.Insert(item);
}
}

/// <summary>
/// 插入数据
/// </summary>
/// <param name="item">插入值</param>
public void InsertAll(IEnumerable<T> list)
{
using (var db = DbConnection)
{
db.InsertAll(list);
}
}

/// <summary>
/// 删除数据
/// </summary>
/// <param name="item">删除值</param>
public void Delete(T item)
{
using (var db = DbConnection)
{
db.Delete(item);
}
}

/// <summary>
/// 更新数据
/// </summary>
/// <param name="item">更新值</param>
public void Update(T item)
{
using (var db = DbConnection)
{
db.Update(item);
}
}

/// <summary>
/// 查询所有数据
/// </summary>
/// <returns>表中所有数据</returns>
public List<T> QueryAll()
{
using (var db = DbConnection)
{
return db.Table<T>().ToList();
}
}
}
}
27 changes: 27 additions & 0 deletions SqliteDemo/SqliteDemo/Models/Person.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
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.Net.Attributes;

namespace SqliteDemo.Models
{
public class Person
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }

public string Name { get; set; }

public string Sex { get; set; }

public int Age { get; set; }
}
}
9 changes: 9 additions & 0 deletions SqliteDemo/SqliteDemo/Properties/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="SqliteDemo.SqliteDemo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="21" />
<application android:allowBackup="true" android:label="@string/app_name">
</application>
</manifest>
Loading

0 comments on commit a96dc8a

Please sign in to comment.