androidsqlite數(shù)據(jù)庫(kù)buttonstringlayout
?
目錄(?)[-]
SQLite簡(jiǎn)介SQLite使用簡(jiǎn)介在DOS環(huán)境對(duì)SQLite數(shù)據(jù)庫(kù)的操作步驟
?
1.SQLite簡(jiǎn)介:
SQLite是一款輕型的數(shù)據(jù)庫(kù),它的設(shè)計(jì)目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常低,能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),同時(shí)可以喝很多種程序語(yǔ)言相結(jié)合,比如PHP、JAVA等。
SQLite雖然很小巧,但是支持的SQL語(yǔ)言不會(huì)遜色于其他開源數(shù)據(jù)庫(kù)。
SQLite數(shù)據(jù)庫(kù)的核心引擎不需要依賴第三方軟件,也不需要所謂的“安裝”。
SQLite數(shù)據(jù)庫(kù)中所有的信息都包含在一個(gè)文件夾內(nèi),方便管理和維護(hù)。
SQLite數(shù)據(jù)庫(kù)通過數(shù)據(jù)庫(kù)級(jí)上的獨(dú)占性和共享鎖來(lái)實(shí)現(xiàn)獨(dú)立的事務(wù)處理。這意味著多個(gè)線程可以再同一時(shí)間從同一數(shù)據(jù)庫(kù)讀取數(shù)據(jù),但只能有一個(gè)可以寫入數(shù)據(jù)。
2.SQLite使用簡(jiǎn)介:
本例子主要完成的功能是:點(diǎn)擊一下按鈕實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的建立、更新;以及對(duì)數(shù)據(jù)的插入、修改、查詢、刪除等功能;
首先自定義一個(gè)類繼承SQLiteOpenHelper類;
<span style="background-color: rgb(255, 153, 255);">package com.douf.android.db;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;public class DatebaseHelper extends SQLiteOpenHelper{//定義要建立的數(shù)據(jù)庫(kù)名;private static final String DBNAME="test1";//定義要建立的數(shù)據(jù)庫(kù)的版本號(hào);private static final int VERSION=1;//構(gòu)造器;分別為:上下文、表名、... 、版本號(hào);public DatebaseHelper(Context context, String name, CursorFactory factory,int version) {super(context, DBNAME, null, version);} //創(chuàng)建數(shù)據(jù)庫(kù)@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),password varchar(20))");}//更新數(shù)據(jù)庫(kù)@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
</span>
?
在main.xml中的配置定義幾個(gè)簡(jiǎn)單的Button:
<span style="background-color: rgb(255, 153, 255);"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical" ><Button android:id="@+id/createButton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/createdatabase"/>
<Button android:id="@+id/updateButton"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/updatedatabase"/>
<Buttonandroid:id="@+id/insert"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/insert"/>
<Buttonandroid:id="@+id/update"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/update"/>
<Buttonandroid:id="@+id/query"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/query"/>
<Buttonandroid:id="@+id/delete"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/delete"/>
</LinearLayout></span>
在string.xml中的一些配置:
<span style="background-color: rgb(255, 153, 255);"><?xml version="1.0" encoding="utf-8"?>
<resources><string name="hello">Hello World, SqliteActivity!</string><string name="app_name">Sqlite</string><string name="createdatabase">創(chuàng)建數(shù)據(jù)庫(kù)</string><string name="updatedatabase">更新數(shù)據(jù)庫(kù)</string><string name="insert">插入數(shù)據(jù)</string><string name="update">更新數(shù)據(jù)</string><string name="query">查詢數(shù)據(jù)</string><string name="delete">刪除數(shù)據(jù)</string></resources></span>
然后定義一個(gè)繼承Activity類來(lái)完成對(duì)數(shù)據(jù)庫(kù)的各種操作:
<span style="background-color: rgb(255, 153, 255);">package com.douf.android;import com.douf.android.db.DatebaseHelper;import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;public class SqliteActivity extends Activity {/** Called when the activity is first created. *///定義main.xml中配置的Buttonprivate Button createButton;private Button updateButton;private Button insert;private Button update;private Button query;private Button delete;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//通過findviewById來(lái)得到那些Button空間;createButton=(Button) findViewById(R.id.createButton);updateButton=(Button) findViewById(R.id.updateButton);insert=(Button) findViewById(R.id.insert);update=(Button) findViewById(R.id.update);query=(Button) findViewById(R.id.query);delete=(Button) findViewById(R.id.delete);//為每個(gè)Button設(shè)置監(jiān)聽器createButton.setOnClickListener(new ButtonListener1());updateButton.setOnClickListener(new ButtonListener2());insert.setOnClickListener(new ButtonListener3());update.setOnClickListener(new ButtonListener4());query.setOnClickListener(new ButtonListener5());delete.setOnClickListener(new ButtonListener6());}//創(chuàng)建數(shù)據(jù)庫(kù)class ButtonListener1 implements OnClickListener{@Overridepublic void onClick(View v) {//創(chuàng)建一個(gè)DatebaseHelper對(duì)象DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 1);//只有調(diào)用了SQLiteOpenHelper的getReadableDatabase或者getWritableDatabase才能真正的創(chuàng)建數(shù)據(jù)庫(kù)SQLiteDatabase db=dbHelper.getReadableDatabase();}}//更新數(shù)據(jù)庫(kù)class ButtonListener2 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelpler=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelpler.getReadableDatabase();}}//插入數(shù)據(jù)class ButtonListener3 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name", "zhangsan");values.put("password", "123456");db.insert("person", null, values);} }//修改數(shù)據(jù)class ButtonListener4 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();ContentValues values=new ContentValues();values.put("name", "zhangsan");db.update("person", values, "id=?", new String[]{"1"});} }//查詢數(shù)據(jù)class ButtonListener5 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getReadableDatabase();Cursor cursor=db.query("person", new String[]{"id","name","password"}, "id=?", new String[]{"1"}, null, null, null);while (cursor.moveToNext()) {int id=cursor.getInt(cursor.getColumnIndex("id"));String name=cursor.getString(cursor.getColumnIndex("name"));String password=cursor.getString(cursor.getColumnIndex("password"));System.out.println(id+"\t"+name+"\t"+password); }}}//刪除數(shù)據(jù)class ButtonListener6 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelper.getWritableDatabase();db.delete("person", "id=?", new String[]{"1"});}}
}</span>
?
3.在DOS環(huán)境對(duì)SQLite數(shù)據(jù)庫(kù)的操作步驟:
adb shell
cd data
cd data
cd com.douf.android 進(jìn)入本實(shí)例的包
cd databases 進(jìn)入此實(shí)例SQLite數(shù)據(jù)庫(kù)
sqlite3 建立的數(shù)據(jù)庫(kù)名
.schema 可以查看此數(shù)據(jù)庫(kù)的
這時(shí)就可以通過簡(jiǎn)單的SQL語(yǔ)句進(jìn)行操作了。
以上就是對(duì)SQLite的簡(jiǎn)單使用。
?
超強(qiáng)干貨來(lái)襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生
總結(jié)
以上是生活随笔為你收集整理的Android学习之SQLite的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。