日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
安卓數(shù)據(jù)庫操作源碼:增刪改查(安卓數(shù)據(jù)庫的增刪改查源碼)

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

成都創(chuàng)新互聯(lián)堅信:善待客戶,將會成為終身客戶。我們能堅持多年,是因為我們一直可值得信賴。我們從不忽悠初訪客戶,我們用心做好本職工作,不忘初心,方得始終。10多年網(wǎng)站建設(shè)經(jīng)驗成都創(chuàng)新互聯(lián)是成都老牌網(wǎng)站營銷服務(wù)商,為您提供成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、H5響應(yīng)式網(wǎng)站、網(wǎng)站制作、品牌網(wǎng)站設(shè)計、微信小程序定制開發(fā)服務(wù),給眾多知名企業(yè)提供過好品質(zhì)的建站服務(wù)。

本文將講解安卓數(shù)據(jù)庫操作的源碼:增刪改查。增刪改查是數(shù)據(jù)庫操作的基本操作,是使用SQLite數(shù)據(jù)庫時必須掌握的操作。

一、創(chuàng)建數(shù)據(jù)庫

在使用SQLite數(shù)據(jù)庫前,需要創(chuàng)建數(shù)據(jù)庫。一般情況下,創(chuàng)建一個新的數(shù)據(jù)庫的方法是通過創(chuàng)建一個SQLiteOpenHelper類實例,然后調(diào)用它的getWritableDatabase()或getReadableDatabase()方法來獲取SQLiteDataBase對象。

以下是一個示例代碼:

“`java

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = “test.db”; //數(shù)據(jù)庫名稱

public static final int DATABASE_VERSION = 1; //數(shù)據(jù)庫版本

//數(shù)據(jù)庫創(chuàng)建語句

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語句,創(chuàng)建數(shù)據(jù)庫

}

@Override

public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}

}

“`

在上面的代碼中,MySQLiteOpenHelper類繼承自SQLiteOpenHelper類。

其中,DATABASE_NAME和DATABASE_VERSION分別表示數(shù)據(jù)庫的名稱和版本號。在MySQLiteOpenHelper的構(gòu)造函數(shù)中,調(diào)用了SQLiteOpenHelper的構(gòu)造函數(shù),并傳入了數(shù)據(jù)庫的名稱和版本號。

CREATE_TABLE語句定義了一個person表,包括id、name和age三個字段。在onCreate()方法中,執(zhí)行了這條CREATE_TABLE語句,創(chuàng)建了數(shù)據(jù)庫。

二、插入數(shù)據(jù)

插入數(shù)據(jù)是數(shù)據(jù)庫操作中最基本的操作之一。以下是一個示例代碼:

“`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ù)的方法。

通過MySQLiteOpenHelper獲取SQLiteDatabase對象。

然后,使用ContentValues構(gòu)建需要插入的數(shù)據(jù)。

調(diào)用db.insert()方法,將數(shù)據(jù)插入到person表中。

三、查詢數(shù)據(jù)

查詢數(shù)據(jù)是數(shù)據(jù)庫操作中另一個基本操作。以下是一個示例代碼:

“`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ù)的方法。

通過MySQLiteOpenHelper獲取SQLiteDatabase對象。

然后,使用db.query()方法,查詢person表中的所有數(shù)據(jù)。由于想要查詢所有數(shù)據(jù),所以參數(shù)傳入null。最后返回的是Cursor對象。

接著,遍歷Cursor對象,通過cursor.getInt()和cursor.getString()方法獲取id、name和age字段的值,并將數(shù)據(jù)封裝到Person對象中。將所有Person對象添加到List中,最后返回List。

注意:使用完Cursor和SQLiteDatabase對象后,需要調(diào)用close()方法進行關(guān)閉操作。

四、修改數(shù)據(jù)

修改數(shù)據(jù)是數(shù)據(jù)庫操作中很重要的一個操作。以下是一個示例代碼:

“`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ù)的方法。

通過MySQLiteOpenHelper獲取SQLiteDatabase對象。

然后,使用ContentValues構(gòu)建需要更新的數(shù)據(jù)。

接著,使用db.update()方法,將更新的數(shù)據(jù)和需要更新的條件傳入。

調(diào)用db.close()方法進行關(guān)閉操作。

五、刪除數(shù)據(jù)

刪除數(shù)據(jù)也是數(shù)據(jù)庫操作中很重要的一個操作。以下是一個示例代碼:

“`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ù)的方法。

通過MySQLiteOpenHelper獲取SQLiteDatabase對象。

然后,使用db.delete()方法,將需要刪除的數(shù)據(jù)的id傳入。

調(diào)用db.close()方法進行關(guān)閉操作。

六、

相關(guān)問題拓展閱讀:

  • C# asp.net WebForm 的三層架構(gòu)配合ListView實現(xiàn)增刪改查源碼

C# asp.net WebForm 的三層架構(gòu)配合ListView實現(xiàn)增刪改查源碼

C# asp.net WebForm 的三層架構(gòu)配合ListView實現(xiàn)增刪改查源碼:

1、用Access新建一個卜陸表MResume,人事管理表:

ID 姓名 性別 出生日期 工作年限 證件類型 證件號 居住地 Email 手機號碼 家庭 圖片 自我評價

2、控件的使用:bindingNavigator(實現(xiàn)分頁功能), dataGridView(顯示數(shù)據(jù))

在C# WinForm 中有這一個app.config的文件,這個文件的作用可以當(dāng)作web程序中的webconfig文件。

這里面可以記錄數(shù)據(jù)庫連接字符串

Access下數(shù)據(jù)庫連接函數(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ù)庫中的數(shù)據(jù)讀到dataGridView讓這個控件來顯示數(shù)據(jù):

private void ResumeTest_Load(object sender, EventArgs e)

{

//手動代碼把數(shù)據(jù)庫中的數(shù)據(jù)顯示出來

OleDbConnection conn = GetConnection();

string sqlText = “select 姓名,性別,出生日期,工作年限,證件類型,證件號,居住地,Email,手機號碼,家庭,自我評價 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; //雙擊不能修改了,這是通過編程的方式來修改單元格內(nèi)容的

this.ComboxSelect.Items.Add(“請選擇類別”);

this.ComboxSelect.Items.Add(“姓名”);

this.ComboxSelect.Items.Add(“性別”);

this.ComboxSelect.SelectedText = “請選擇好碧類別”;

}

更新代碼如下:

private void 修改ToolStripMenuItem_Click(object sender, EventArgs e)

{

dataGridView1_DoubleClick(sender, e);

//類似于dataGridView的更新操作,也就是雙擊操作

}

private void dataGridView1_CellMouseDown(object

DataGridViewCellMouseEventArgs e)

{

//判斷如果點擊的是鼠標(biāo)右鍵

if (e.Button == MouseButtons.Right)

{

//判斷鼠標(biāo)點擊在數(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(“確定要刪除這條記錄嗎?”, “提示”, 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)

{

//首先進行模糊查詢

string strComboxSelect = ComboxSelect.Text.Trim();

string strSearch = txtSearch.Text.Trim();

if(strComboxSelect.Equals(“請選擇類別”))

{

MessageBox.Show(“請選擇類別!”,”提示”);

return;

}

if (strSearch == “” || strSearch == null)

{

MessageBox.Show(“請輸入查詢內(nèi)容!”, “提示”);

return;

}

//手動代碼把數(shù)據(jù)庫中的數(shù)據(jù)顯示出來

OleDbConnection conn = GetConnection();

string sqlText = “select 姓名,性別,出生日期,工作年限,證件類型,證件號,居住地,Email,手機號碼,家庭,自我評價 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ù)庫的增刪改查源碼的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于安卓數(shù)據(jù)庫的增刪改查源碼,安卓數(shù)據(jù)庫操作源碼:增刪改查,C# asp.net WebForm 的三層架構(gòu)配合ListView實現(xiàn)增刪改查源碼的信息別忘了在本站進行查找喔。

香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機、虛擬主機、域名注冊、VPS主機、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。


網(wǎng)頁題目:安卓數(shù)據(jù)庫操作源碼:增刪改查(安卓數(shù)據(jù)庫的增刪改查源碼)
分享路徑:http://www.5511xx.com/article/dpsspis.html