日韩无码专区无码一级三级片|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ù)庫操作,實現(xiàn)數(shù)據(jù)存儲和管理(安卓允許數(shù)據(jù)庫)

安卓作為一種流行的移動操作系統(tǒng),為開發(fā)者提供了極大的靈活性和自由度。其中,數(shù)據(jù)庫操作是安卓開發(fā)中非常重要的一部分,它可以實現(xiàn)數(shù)據(jù)的存儲和管理。

創(chuàng)新互聯(lián)建站是一家專注于成都做網(wǎng)站、網(wǎng)站建設(shè)與策劃設(shè)計,建甌網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)建站做網(wǎng)站,專注于網(wǎng)站建設(shè)十載,網(wǎng)設(shè)計領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:建甌等地區(qū)。建甌做網(wǎng)站價格咨詢:028-86922220

安卓是基于Java語言開發(fā)的,因此也支持Java中常見的數(shù)據(jù)庫操作方式。安卓提供了自己的數(shù)據(jù)庫管理系統(tǒng)——SQLite,這是一個輕量級的關(guān)系數(shù)據(jù)庫管理系統(tǒng),具有占用空間小、占用資源少、速度快等優(yōu)點。在安卓中使用SQLite可以實現(xiàn)存儲、讀取和修改數(shù)據(jù)等功能,比如存儲用戶的賬號、密碼、聯(lián)系方式等。

SQLite的使用簡單便捷,以下是一個SQLite的使用案例:

1. 創(chuàng)建數(shù)據(jù)庫幫助類

“`

public class DBHelper extends SQLiteOpenHelper {

// 定義數(shù)據(jù)庫名稱

public static final String DATABASE_NAME = “demo.db”;

// 定義數(shù)據(jù)庫版本號

public static final int DATABASE_VERSION = 1;

// 定義表名和列名

public static final String TABLE_NAME = “user_info”;

public static final String COLUMN_ID = “_id”;

public static final String COLUMN_USER_NAME = “user_name”;

public static final String COLUMN_PASSWORD = “password”;

public static final String COLUMN_MOBILE = “mobile”;

public DBHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override

public void onCreate(SQLiteDatabase db) {

// 創(chuàng)建用戶信息表

String CREATE_TABLE = “CREATE TABLE ” + TABLE_NAME + “(“

+ COLUMN_ID + ” INTEGER PRIMARY KEY AUTOINCREMENT,”

+ COLUMN_USER_NAME + ” TEXT,”

+ COLUMN_PASSWORD + ” TEXT,”

+ COLUMN_MOBILE + ” TEXT” + “)”;

db.execSQL(CREATE_TABLE);

}

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

// 升級數(shù)據(jù)庫表結(jié)構(gòu)

String DROP_TABLE = “DROP TABLE IF EXISTS ” + TABLE_NAME;

db.execSQL(DROP_TABLE);

onCreate(db);

}

}

“`

2. 在Activity中使用SQLite

“`

public class MnActivity extends AppCompatActivity {

private DBHelper dbHelper;

private SQLiteDatabase db;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_mn);

dbHelper = new DBHelper(this);

db = dbHelper.getWritableDatabase();

}

// 添加用戶信息

private void addUser(String userName, String password, String mobile) {

ContentValues values = new ContentValues();

values.put(DBHelper.COLUMN_USER_NAME, userName);

values.put(DBHelper.COLUMN_PASSWORD, password);

values.put(DBHelper.COLUMN_MOBILE, mobile);

long rowId = db.insert(DBHelper.TABLE_NAME, null, values);

}

// 查詢用戶信息

private void queryUser() {

Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);

if (cursor.moveToFirst()) {

do {

String userName = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_USER_NAME));

String password = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_PASSWORD));

String mobile = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_MOBILE));

Log.i(“MnActivity”, “userName = ” + userName + “, password = ” + password + “, mobile = ” + mobile);

} while (cursor.moveToNext());

}

cursor.close();

}

// 更新用戶信息

private void updateUser(String userName, String password, String mobile) {

ContentValues values = new ContentValues();

values.put(DBHelper.COLUMN_PASSWORD, password);

values.put(DBHelper.COLUMN_MOBILE, mobile);

db.update(DBHelper.TABLE_NAME, values, DBHelper.COLUMN_USER_NAME + “=?”, new String[]{userName});

}

// 刪除用戶信息

private void deleteUser(String userName) {

db.delete(DBHelper.TABLE_NAME, DBHelper.COLUMN_USER_NAME + “=?”, new String[]{userName});

}

}

“`

在上述代碼中,我們定義了一個DBHelper類來管理數(shù)據(jù)庫,包含了創(chuàng)建數(shù)據(jù)庫和升級數(shù)據(jù)庫的方法。我們還定義了一些常量,如表名和列名,方便我們在代碼中使用。在Activity中,我們可以通過DBHelper獲取到數(shù)據(jù)庫實例,然后可以進(jìn)行添加、查詢、更新甚至刪除數(shù)據(jù)的操作。

是一項非常重要的開發(fā)技能。通過SQLite,我們可以輕松地實現(xiàn)數(shù)據(jù)的持久化存儲,為用戶提供更加完善的應(yīng)用體驗。學(xué)習(xí)和掌握SQLite的使用,對于提高安卓開發(fā)能力和開發(fā)效率都是非常有幫助的。

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

  • 安卓開發(fā)連接數(shù)據(jù)庫

安卓開發(fā)連接數(shù)據(jù)庫

Android 連接數(shù)據(jù)庫

Android采用關(guān)系型數(shù)據(jù)庫SQLite3,它是一個支持SQL輕量級的嵌入式數(shù)據(jù)庫,在嵌入式操作上有很廣泛的,WM采用的也是SQLite3

關(guān)于過于、原理方面的東西在這篇文章里不會提到,但是如果你想能夠快速的學(xué)會操作SQLite3,那這就是你要找的文章!

首先,我辯慶虧們看一下api,所有數(shù)據(jù)庫相關(guān)的接口、類都在。database和android.database.sqlite兩個包攜神下,雖然只有兩個包,但是如果你英文不好或是太懶的話也要迷茫一段時間,其實,我們真正用的到的沒有幾個!

1、SQLiteOpenHelper (android.database.sqlite.SQLiteOpenHelper)

這是一個抽象類,關(guān)于抽象類我們都知道,如果要使用它,一定是繼承它!

這個類的方法很少,有一個構(gòu)造方法

SQLiteOpenHelper(android.content.Context context, java.lang.String name,android.database.sqlite.SQLiteDatabase.CursorFactory factory, int version);

參數(shù)不做過多的解釋,CursorFactory一般直接傳null就可以

public void onCreate(SQLiteDatabase db)

此方法在創(chuàng)建數(shù)據(jù)庫是被調(diào)用,所以,應(yīng)該把創(chuàng)建表的操作放到這個方法里面,一會兒在后面我們會再詳細(xì)的說如何創(chuàng)建表

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

從方法名上我們就能知道這個方法是執(zhí)行更新的,沒錯,當(dāng)version改變是系統(tǒng)會調(diào)用這個方法,所差猛以在這個方法里應(yīng)該執(zhí)行刪除現(xiàn)有表,然后手動調(diào)用onCreate的操作

SQLiteDatabase getReadableDatabase()

可讀的SQLiteDatabase對象

SQLiteDatabase getWritableDatabase()

獲取可寫的SQLiteDatabase對象

2、SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

關(guān)于操作數(shù)據(jù)庫的工作(增、刪、查、改)都在這個類里

execSQL(sql)

執(zhí)行SQL語句,用這個方法+SQL語句可以非常方便的執(zhí)行增、刪、查、改

除此之外,Android還提供了功過方法實現(xiàn)增、刪、查、改

long insert(TABLE_NAME, null, contentValues)添加記錄

int delete(TABLE_NAME, where, whereValue)刪除記錄

int update(TABLE_NAME, contentValues, where, whereValue) 更新記錄

Cursor query(TABLE_NAME, null, null, null, null, null, null) 查詢記錄

除此之外,還有很多方法,如:beginTransaction()開始事務(wù)、endTransaction()結(jié)束事務(wù)…有興趣的可以自己看api,這里就不多贅述了

3、Cursor(android.database.Cursor)

游標(biāo)(接口),這個很熟悉了吧,Cursor里的方法非常多,常用的有:

boolean moveToPosition(position)將指針移動到某記錄

getColumnIndex(Contacts.People.NAME)按列名獲取id

int getCount()獲取記錄總數(shù)

boolean requery()重新查詢

boolean isAfterLast()指針是否在末尾

boolean isBeforeFirst()時候是開始位置

boolean isFirst()是否是之一條記錄

boolean isLast()是否是最后一條記錄

boolean moveToFirst()、 boolean moveToLast()、 boolean moveToNext()同moveToPosition(position)

4、SimpleCursorAdapter(android.widget.SimpleCursorAdapter)

也許你會奇怪了,之前我還說過關(guān)于數(shù)據(jù)庫的操作都在database和database.sqlite包下,為什么把一個Adapter放到這里,如果你用過Android的SQLite3,你一定會知道

,這是因為我們對數(shù)據(jù)庫的操作會經(jīng)常跟列表聯(lián)系起來

經(jīng)常有朋友會在這出錯,但其實也很簡單

SimpleCursorAdapter adapter = new SimpleCursorAdapter(

this,

R.layout.list,

myCursor,

new String {DB.TEXT1,DB. TEXT2},

new int{ R.id.list1,R.id.listText2 });

my.setAdapter(adapter);

一共5個參數(shù),具體如下:

參數(shù)1:Content

參數(shù)2:布局

參數(shù)3:Cursor游標(biāo)對象

參數(shù)4:顯示的字段,傳入String

參數(shù)5:顯示字段使用的組件,傳入int,該數(shù)組中是TextView組件的id

到這里,關(guān)于數(shù)據(jù)庫的操作就結(jié)束了,但是到目前為止我只做了翻譯的工作,有些同學(xué)可能還是沒有掌握,放心,下面我們一起順著正常開發(fā)的思路理清一下頭緒!

前面的只是幫沒做過的朋友做下普及,下面才是你真正需要的!

一、寫一個類繼承SQLiteOpenHelpe

public class DatabaseHelper extends SQLiteOpenHelper

構(gòu)造方法:

DatabaseHelper(Context context) {

super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

在onCreate方法里寫建表的操作

public void onCreate(SQLiteDatabase db) {

String sql = “CREATE TABLE tb_test (_id INTEGER DEFAULT ‘1’ NOT NULL PRIMARY KEY AUTOINCREMENT,class_jb TEXT NOT NULL,class_yj TEXT NOT NULL,title TEXT NOT NULL,content_yj TEXT NOT NULL)”;

db.execSQL(sql);//需要異常捕獲

}

在onUpgrade方法里刪除現(xiàn)有表,然后手動調(diào)用onCtreate創(chuàng)建表

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String sql = “drop table ”+tbname;

db.execSQL(sql);

onCreate(db);

}

對表增、刪、查、改的方法,這里用的是SQLiteOpenHelper提供的方法,也可以用sql語句實現(xiàn),都是一樣的

關(guān)于獲取可讀/可寫SQLiteDatabase,我不說大家也應(yīng)該會想到,只有查找才會用到可讀的SQLiteDatabase

/**

* 添加數(shù)據(jù)

*/

public long insert(String tname, int tage, String ttel){

SQLiteDatabase db= getWritableDatabase();//獲取可寫SQLiteDatabase對象

//ContentValues類似map,存入的是鍵值對

ContentValues contentValues = new ContentValues();

contentValues.put(“tname”, tname);

contentValues.put(“tage”, tage);

contentValues.put(“ttel”, ttel);

return db.insert(tbname, null, contentValues);

}

/**

* 刪除記錄

* @param _id

*/

public void delete(String _id){

SQLiteDatabase db= getWritableDatabase();

db.delete(tbname,

“_id=?”,

new String{_id});

}

/**

* 更新記錄的,跟插入的很像

*/

public void update(String _id,String tname, int tage, String ttel){

SQLiteDatabase db= getWritableDatabase();

ContentValues contentValues = new ContentValues();

contentValues.put(“tname”, tname);

contentValues.put(“tage”, tage);

contentValues.put(“ttel”, ttel);

db.update(tbname, contentValues,

“_id=?”,

new String{_id});

}

/**

* 查詢所有數(shù)據(jù)

* @return Cursor

*/

public Cursor select(){

SQLiteDatabase db = getReadableDatabase();

return db.query(

tbname,

new String{“_id”,“tname”,“tage”,“ttel”,“taddr”},

null,

null, null, null, “_id desc”);

}

關(guān)于db.query方法的參數(shù),有很多,為了防止大家弄亂,我簡單說一下

參數(shù)1:表名

參數(shù)2:返回數(shù)據(jù)包含的列信息,String數(shù)組里放的都是列名

參數(shù)3:相當(dāng)于sql里的where,sql里where后寫的內(nèi)容放到這就行了,例如:tage>?

參數(shù)4:如果你在參數(shù)3里寫了?(知道我為什么寫tage>?了吧),那個這里就是代替?的值 接上例:new String{“30”}

參數(shù)5:分組,不解釋了,不想分組就傳null

參數(shù)6:having,想不起來的看看SQL

參數(shù)7:orderBy排序

到這里,你已經(jīng)完成了最多的之一步!我們來看看都用到了那些類:

SQLiteOpenHelper我們繼承使用的

SQLiteDatabase增刪查改都離不開它,即使你直接用sql語句,也要用到execSQL(sql)

二、這里無非是對DatabaseHelper類定義方法的調(diào)用,沒什么可說的,不過我還是對查詢再嘮叨幾句吧

Android查詢出來的結(jié)果一Cursor形式返回

cursor = sqLiteHelper.select();//是不是很簡單?

查詢出來的cursor一般會顯示在listView中,這就要用到剛才提到的SimpleCursorAdapter

SimpleCursorAdapter adapter = new SimpleCursorAdapter(

this,

R.layout.list_row,

cursor,

new String{“tname”,“ttel”},

new int{R.id.TextView01,R.id.TextView02}

);

里面帶有實例。自己好好學(xué)習(xí)吧!

安卓允許數(shù)據(jù)庫的介紹就聊到這里吧,感謝你花時間閱讀本站內(nèi)容,更多關(guān)于安卓允許數(shù)據(jù)庫,安卓允許數(shù)據(jù)庫操作,實現(xiàn)數(shù)據(jù)存儲和管理,安卓開發(fā)連接數(shù)據(jù)庫的信息別忘了在本站進(jìn)行查找喔。

成都創(chuàng)新互聯(lián)科技有限公司,是一家專注于互聯(lián)網(wǎng)、IDC服務(wù)、應(yīng)用軟件開發(fā)、網(wǎng)站建設(shè)推廣的公司,為客戶提供互聯(lián)網(wǎng)基礎(chǔ)服務(wù)!
創(chuàng)新互聯(lián)(www.cdcxhl.com)提供簡單好用,價格厚道的香港/美國云服務(wù)器和獨(dú)立服務(wù)器。創(chuàng)新互聯(lián)成都老牌IDC服務(wù)商,專注四川成都IDC機(jī)房服務(wù)器托管/機(jī)柜租用。為您精選優(yōu)質(zhì)idc數(shù)據(jù)中心機(jī)房租用、服務(wù)器托管、機(jī)柜租賃、大帶寬租用,可選線路電信、移動、聯(lián)通等。


新聞標(biāo)題:安卓允許數(shù)據(jù)庫操作,實現(xiàn)數(shù)據(jù)存儲和管理(安卓允許數(shù)據(jù)庫)
鏈接URL:http://www.5511xx.com/article/djgpeds.html