androidsqlite數據庫buttonstringlayout
?
目錄(?)[-]
SQLite簡介SQLite使用簡介在DOS環境對SQLite數據庫的操作步驟
?
1.SQLite簡介:
SQLite是一款輕型的數據庫,它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它占用資源非常低,能夠支持Windows/Linux/Unix等等主流的操作系統,同時可以喝很多種程序語言相結合,比如PHP、JAVA等。
SQLite雖然很小巧,但是支持的SQL語言不會遜色于其他開源數據庫。
SQLite數據庫的核心引擎不需要依賴第三方軟件,也不需要所謂的“安裝”。
SQLite數據庫中所有的信息都包含在一個文件夾內,方便管理和維護。
SQLite數據庫通過數據庫級上的獨占性和共享鎖來實現獨立的事務處理。這意味著多個線程可以再同一時間從同一數據庫讀取數據,但只能有一個可以寫入數據。
2.SQLite使用簡介:
本例子主要完成的功能是:點擊一下按鈕實現對數據庫的建立、更新;以及對數據的插入、修改、查詢、刪除等功能;
首先自定義一個類繼承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{//定義要建立的數據庫名;private static final String DBNAME="test1";//定義要建立的數據庫的版本號;private static final int VERSION=1;//構造器;分別為:上下文、表名、... 、版本號;public DatebaseHelper(Context context, String name, CursorFactory factory,int version) {super(context, DBNAME, null, version);} //創建數據庫@Overridepublic void onCreate(SQLiteDatabase db) {db.execSQL("create table person(id integer primary key autoincrement,name varchar(20),password varchar(20))");}//更新數據庫@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
</span>
?
在main.xml中的配置定義幾個簡單的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">創建數據庫</string><string name="updatedatabase">更新數據庫</string><string name="insert">插入數據</string><string name="update">更新數據</string><string name="query">查詢數據</string><string name="delete">刪除數據</string></resources></span>
然后定義一個繼承Activity類來完成對數據庫的各種操作:
<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來得到那些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);//為每個Button設置監聽器createButton.setOnClickListener(new ButtonListener1());updateButton.setOnClickListener(new ButtonListener2());insert.setOnClickListener(new ButtonListener3());update.setOnClickListener(new ButtonListener4());query.setOnClickListener(new ButtonListener5());delete.setOnClickListener(new ButtonListener6());}//創建數據庫class ButtonListener1 implements OnClickListener{@Overridepublic void onClick(View v) {//創建一個DatebaseHelper對象DatebaseHelper dbHelper=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 1);//只有調用了SQLiteOpenHelper的getReadableDatabase或者getWritableDatabase才能真正的創建數據庫SQLiteDatabase db=dbHelper.getReadableDatabase();}}//更新數據庫class ButtonListener2 implements OnClickListener{@Overridepublic void onClick(View v) {DatebaseHelper dbHelpler=new DatebaseHelper(SqliteActivity.this, "DBNAME", null, 3);SQLiteDatabase db=dbHelpler.getReadableDatabase();}}//插入數據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);} }//修改數據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"});} }//查詢數據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); }}}//刪除數據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環境對SQLite數據庫的操作步驟:
adb shell
cd data
cd data
cd com.douf.android 進入本實例的包
cd databases 進入此實例SQLite數據庫
sqlite3 建立的數據庫名
.schema 可以查看此數據庫的
這時就可以通過簡單的SQL語句進行操作了。
以上就是對SQLite的簡單使用。
?
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生
總結
以上是生活随笔為你收集整理的Android学习之SQLite的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。