新聞中心
在Android開發(fā)中,數(shù)據(jù)庫(kù)是必不可少的一部分。很多應(yīng)用程序需要存儲(chǔ)和管理數(shù)據(jù),這就需要涉及到數(shù)據(jù)庫(kù)的操作。安卓提供了SQLite數(shù)據(jù)庫(kù),是輕量級(jí)的數(shù)據(jù)庫(kù)引擎,非常適合移動(dòng)應(yīng)用程序的嵌入式數(shù)據(jù)庫(kù)。

成都創(chuàng)新互聯(lián)堅(jiān)信:善待客戶,將會(huì)成為終身客戶。我們能堅(jiān)持多年,是因?yàn)槲覀円恢笨芍档眯刨嚒N覀儚牟缓鲇瞥踉L客戶,我們用心做好本職工作,不忘初心,方得始終。10多年網(wǎng)站建設(shè)經(jīng)驗(yàn)成都創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營(yíng)銷服務(wù)商,為您提供成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、H5響應(yīng)式網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站設(shè)計(jì)、微信小程序定制開發(fā)服務(wù),給眾多知名企業(yè)提供過(guò)好品質(zhì)的建站服務(wù)。
本文將講解安卓數(shù)據(jù)庫(kù)操作的源碼:增刪改查。增刪改查是數(shù)據(jù)庫(kù)操作的基本操作,是使用SQLite數(shù)據(jù)庫(kù)時(shí)必須掌握的操作。
一、創(chuàng)建數(shù)據(jù)庫(kù)
在使用SQLite數(shù)據(jù)庫(kù)前,需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)。一般情況下,創(chuàng)建一個(gè)新的數(shù)據(jù)庫(kù)的方法是通過(guò)創(chuàng)建一個(gè)SQLiteOpenHelper類實(shí)例,然后調(diào)用它的getWritableDatabase()或getReadableDatabase()方法來(lái)獲取SQLiteDataBase對(duì)象。
以下是一個(gè)示例代碼:
“`java
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = “test.db”; //數(shù)據(jù)庫(kù)名稱
public static final int DATABASE_VERSION = 1; //數(shù)據(jù)庫(kù)版本
//數(shù)據(jù)庫(kù)創(chuàng)建語(yǔ)句
private static final String CREATE_TABLE = “create table if not exists “
+ “person (id integer primary key autoincrement, name varchar(20), age integer)”;
public MySQLiteOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE); //執(zhí)行SQL語(yǔ)句,創(chuàng)建數(shù)據(jù)庫(kù)
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
“`
在上面的代碼中,MySQLiteOpenHelper類繼承自SQLiteOpenHelper類。
其中,DATABASE_NAME和DATABASE_VERSION分別表示數(shù)據(jù)庫(kù)的名稱和版本號(hào)。在MySQLiteOpenHelper的構(gòu)造函數(shù)中,調(diào)用了SQLiteOpenHelper的構(gòu)造函數(shù),并傳入了數(shù)據(jù)庫(kù)的名稱和版本號(hào)。
CREATE_TABLE語(yǔ)句定義了一個(gè)person表,包括id、name和age三個(gè)字段。在onCreate()方法中,執(zhí)行了這條CREATE_TABLE語(yǔ)句,創(chuàng)建了數(shù)據(jù)庫(kù)。
二、插入數(shù)據(jù)
插入數(shù)據(jù)是數(shù)據(jù)庫(kù)操作中最基本的操作之一。以下是一個(gè)示例代碼:
“`java
public class PersonDao {
private MySQLiteOpenHelper mySQLiteOpenHelper;
public PersonDao(Context context) {
mySQLiteOpenHelper = new MySQLiteOpenHelper(context);
}
/**
* 插入一條數(shù)據(jù)
* @param person 待插入的數(shù)據(jù)
*/
public void insert(Person person) {
SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“name”, person.getName());
values.put(“age”, person.getAge());
db.insert(“person”, null, values);
db.close();
}
}
“`
在上面的代碼中,insert()方法是向person表中插入一條數(shù)據(jù)的方法。
通過(guò)MySQLiteOpenHelper獲取SQLiteDatabase對(duì)象。
然后,使用ContentValues構(gòu)建需要插入的數(shù)據(jù)。
調(diào)用db.insert()方法,將數(shù)據(jù)插入到person表中。
三、查詢數(shù)據(jù)
查詢數(shù)據(jù)是數(shù)據(jù)庫(kù)操作中另一個(gè)基本操作。以下是一個(gè)示例代碼:
“`java
public class PersonDao {
private MySQLiteOpenHelper mySQLiteOpenHelper;
public PersonDao(Context context) {
mySQLiteOpenHelper = new MySQLiteOpenHelper(context);
}
/**
* 查詢所有數(shù)據(jù)
* @return 所有數(shù)據(jù)的
*/
public List queryAll() {
SQLiteDatabase db = mySQLiteOpenHelper.getReadableDatabase();
Cursor cursor = db.query(“person”, null, null, null, null, null, null);
List personList = new ArrayList();
while (cursor.moveToNext()) {
Person person = new Person();
person.setId(cursor.getInt(cursor.getColumnIndex(“id”)));
person.setName(cursor.getString(cursor.getColumnIndex(“name”)));
person.setAge(cursor.getInt(cursor.getColumnIndex(“age”)));
personList.add(person);
}
cursor.close();
db.close();
return personList;
}
}
“`
在上面的代碼中,queryAll()方法是查詢所有用戶數(shù)據(jù)的方法。
通過(guò)MySQLiteOpenHelper獲取SQLiteDatabase對(duì)象。
然后,使用db.query()方法,查詢person表中的所有數(shù)據(jù)。由于想要查詢所有數(shù)據(jù),所以參數(shù)傳入null。最后返回的是Cursor對(duì)象。
接著,遍歷Cursor對(duì)象,通過(guò)cursor.getInt()和cursor.getString()方法獲取id、name和age字段的值,并將數(shù)據(jù)封裝到Person對(duì)象中。將所有Person對(duì)象添加到List中,最后返回List。
注意:使用完Cursor和SQLiteDatabase對(duì)象后,需要調(diào)用close()方法進(jìn)行關(guān)閉操作。
四、修改數(shù)據(jù)
修改數(shù)據(jù)是數(shù)據(jù)庫(kù)操作中很重要的一個(gè)操作。以下是一個(gè)示例代碼:
“`java
public class PersonDao {
private MySQLiteOpenHelper mySQLiteOpenHelper;
public PersonDao(Context context) {
mySQLiteOpenHelper = new MySQLiteOpenHelper(context);
}
/**
* 更新數(shù)據(jù)
* @param person 待更新的數(shù)據(jù)
*/
public void update(Person person) {
SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“name”, person.getName());
values.put(“age”, person.getAge());
db.update(“person”, values, “id=?”, new String[]{String.valueOf(person.getId())});
db.close();
}
}
“`
在上面的代碼中,update()方法是更新person表中的數(shù)據(jù)的方法。
通過(guò)MySQLiteOpenHelper獲取SQLiteDatabase對(duì)象。
然后,使用ContentValues構(gòu)建需要更新的數(shù)據(jù)。
接著,使用db.update()方法,將更新的數(shù)據(jù)和需要更新的條件傳入。
調(diào)用db.close()方法進(jìn)行關(guān)閉操作。
五、刪除數(shù)據(jù)
刪除數(shù)據(jù)也是數(shù)據(jù)庫(kù)操作中很重要的一個(gè)操作。以下是一個(gè)示例代碼:
“`java
public class PersonDao {
private MySQLiteOpenHelper mySQLiteOpenHelper;
public PersonDao(Context context) {
mySQLiteOpenHelper = new MySQLiteOpenHelper(context);
}
/**
* 刪除數(shù)據(jù)
* @param person 待刪除的數(shù)據(jù)
*/
public void delete(Person person) {
SQLiteDatabase db = mySQLiteOpenHelper.getWritableDatabase();
db.delete(“person”, “id=?”, new String[]{String.valueOf(person.getId())});
db.close();
}
}
“`
在上面的代碼中,delete()方法是刪除person表中的數(shù)據(jù)的方法。
通過(guò)MySQLiteOpenHelper獲取SQLiteDatabase對(duì)象。
然后,使用db.delete()方法,將需要?jiǎng)h除的數(shù)據(jù)的id傳入。
調(diào)用db.close()方法進(jìn)行關(guān)閉操作。
六、
相關(guān)問(wèn)題拓展閱讀:
- C# asp.net WebForm 的三層架構(gòu)配合ListView實(shí)現(xiàn)增刪改查源碼
C# asp.net WebForm 的三層架構(gòu)配合ListView實(shí)現(xiàn)增刪改查源碼
C# asp.net WebForm 的三層架構(gòu)配合ListView實(shí)現(xiàn)增刪改查源碼:
1、用Access新建一個(gè)卜陸表MResume,人事管理表:
ID 姓名 性別 出生日期 工作年限 證件類型 證件號(hào) 居住地 Email 手機(jī)號(hào)碼 家庭 圖片 自我評(píng)價(jià)
2、控件的使用:bindingNavigator(實(shí)現(xiàn)分頁(yè)功能), dataGridView(顯示數(shù)據(jù))
在C# WinForm 中有這一個(gè)app.config的文件,這個(gè)文件的作用可以當(dāng)作web程序中的webconfig文件。
這里面可以記錄數(shù)據(jù)庫(kù)連接字符串
Access下數(shù)據(jù)庫(kù)連接函數(shù):
public static OleDbConnection GetConnection()
{
OleDbConnection conn = null;
string strconnectionString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + AppDomain.CurrentDomain.BaseDirectory + “database\\chinabase.mdb;Persist Security Info=True”;
try
{
conn = new OleDbConnection(strconnectionString);
}
catch (Exception ex)
{
throw ex;
}
return conn;
}
3、把數(shù)據(jù)庫(kù)中的數(shù)據(jù)讀到dataGridView讓這個(gè)控件來(lái)顯示數(shù)據(jù):
private void ResumeTest_Load(object sender, EventArgs e)
{
//手動(dòng)代碼把數(shù)據(jù)庫(kù)中的數(shù)據(jù)顯示出來(lái)
OleDbConnection conn = GetConnection();
string sqlText = “select 姓名,性別,出生日期,工作年限,證件類型,證件號(hào),居住地,Email,手機(jī)號(hào)碼,家庭,自我評(píng)價(jià) from MResume order by id asc”;
OleDbCommand cmd = new OleDbCommand(sqlText, conn);
try
{
conn.Open();
//int i = cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter(sqlText, conn);
DataSet ds = new DataSet();
// oda.Fill(dt);
// dataGridView1.DataSource = dt;
oda.Fill(ds, “ds”型襪頃);
dtInfo.Clear();
//dtInfo = null;
dtInfo = ds.Tables;
InitDataSet(dtInfo); //初始化數(shù)據(jù)
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
//設(shè)置GridView樣式
// SetUpDataGridView();
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; //使用戶能夠選擇行
this.dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically; //雙擊不能修改了,這是通過(guò)編程的方式來(lái)修改單元格內(nèi)容的
this.ComboxSelect.Items.Add(“請(qǐng)選擇類別”);
this.ComboxSelect.Items.Add(“姓名”);
this.ComboxSelect.Items.Add(“性別”);
this.ComboxSelect.SelectedText = “請(qǐng)選擇好碧類別”;
}
更新代碼如下:
private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)
{
dataGridView1_DoubleClick(sender, e);
//類似于dataGridView的更新操作,也就是雙擊操作
}
private void dataGridView1_CellMouseDown(object
DataGridViewCellMouseEventArgs e)
{
//判斷如果點(diǎn)擊的是鼠標(biāo)右鍵
if (e.Button == MouseButtons.Right)
{
//判斷鼠標(biāo)點(diǎn)擊在數(shù)據(jù)行上
if (e.RowIndex >= 0)
{
dataGridView1.ClearSelection();
dataGridView1.Rows.Selected = true;
dataGridView1.CurrentCell
dataGridView1.Rows.Cells;
}
}
}
刪除代碼如下:
public bool deletDataGridViewOneLine(object sender, EventArgs e)
{
bool result = false;
Int32 selectedRowCount
dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
int selectedRow = dataGridView1.SelectedRows.Index; //獲得選中的某行
string MName = dataGridView1.Rows.Cells.Value.ToString().Trim();
// MessageBox.Show(MName.ToString());
DialogResult dr = MessageBox.Show(“確定要?jiǎng)h除這條記錄嗎?”, “提示”, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
if (MName != null && MName != “”)
{
OleDbConnection conn = GetConnection();
string sqlText = “delete from MResume where 姓名=@MName”;
OleDbCommand cmd = new OleDbCommand(sqlText, conn);
cmd.Parameters.AddWithValue(“@MName”, MName);
try
{
conn.Open();
int i = cmd.ExecuteNonQuery();
result = true;
}
catch (Exception ex)
{
MessageBox.Show(“發(fā)生異常:” + ex.ToString(), “提示”);
}
查詢代碼如下:
private void btnSelect_Click(object sender, EventArgs e)
{
//首先進(jìn)行模糊查詢
string strComboxSelect = ComboxSelect.Text.Trim();
string strSearch = txtSearch.Text.Trim();
if(strComboxSelect.Equals(“請(qǐng)選擇類別”))
{
MessageBox.Show(“請(qǐng)選擇類別!”,”提示”);
return;
}
if (strSearch == “” || strSearch == null)
{
MessageBox.Show(“請(qǐng)輸入查詢內(nèi)容!”, “提示”);
return;
}
//手動(dòng)代碼把數(shù)據(jù)庫(kù)中的數(shù)據(jù)顯示出來(lái)
OleDbConnection conn = GetConnection();
string sqlText = “select 姓名,性別,出生日期,工作年限,證件類型,證件號(hào),居住地,Email,手機(jī)號(hào)碼,家庭,自我評(píng)價(jià) from MResume where ” + strComboxSelect + ” like ‘%”+@strSearch+”%'”;
OleDbCommand cmd = new OleDbCommand(sqlText, conn);
cmd.Parameters.AddWithValue(“@strSearch”, strSearch);
try
{
conn.Open();
//int i = cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
OleDbDataAdapter oda = new OleDbDataAdapter(sqlText, conn);
oda.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
安卓數(shù)據(jù)庫(kù)的增刪改查源碼的介紹就聊到這里吧,感謝你花時(shí)間閱讀本站內(nèi)容,更多關(guān)于安卓數(shù)據(jù)庫(kù)的增刪改查源碼,安卓數(shù)據(jù)庫(kù)操作源碼:增刪改查,C# asp.net WebForm 的三層架構(gòu)配合ListView實(shí)現(xiàn)增刪改查源碼的信息別忘了在本站進(jìn)行查找喔。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過(guò)10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗(yàn)。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊(cè)、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
新聞名稱:安卓數(shù)據(jù)庫(kù)操作源碼:增刪改查(安卓數(shù)據(jù)庫(kù)的增刪改查源碼)
網(wǎng)站地址:http://www.dlmjj.cn/article/dpsspis.html
其他資訊
- 如何在Oracle數(shù)據(jù)庫(kù)中修改時(shí)間格式 (oracle 修改數(shù)據(jù)庫(kù)時(shí)間格式)
- Linux多核CPU中斷:提高效能、減少延遲(linux多核CPU中斷)
- 一庫(kù)多站:使用單一數(shù)據(jù)庫(kù)支持多個(gè)網(wǎng)站的優(yōu)勢(shì) (多網(wǎng)站使用同一個(gè)數(shù)據(jù)庫(kù))
- 創(chuàng)新互聯(lián)Flask教程:Flask激發(fā)請(qǐng)求發(fā)送前后的調(diào)用
- 一個(gè)虛擬主機(jī)可以放幾個(gè)網(wǎng)站


咨詢
建站咨詢
