使用SQL語句操做SQLite數據庫

先上一張效果圖: java

每次點擊「插入」,都會在下面的ListView中新增一行 android

 

本文涉及四個文件,分別是DBTest.java、main.xml、line.xml、strings.ml 數據庫

DBTest.java ide

public class DBTest extends Activity {

	SQLiteDatabase db;
	Button btn = null;
	ListView listView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		//獲取路徑,也算是一種調試的方法吧
		Log.d("myLog", this.getFilesDir().toString());
//建立或打開數據庫(此處須要使用據對路徑)
		db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir()
				.toString() + "/my.db3", null);
		listView = (ListView) findViewById(R.id.show);
		btn = (Button) findViewById(R.id.ok);
		btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View source) {
				//獲取用戶輸入
				String title = ((EditText)findViewById(R.id.title))
						.getText().toString();
				String content = ((EditText)findViewById(R.id.content))
						.getText().toString();
//若是沒有數據庫表就執行catch語句,先建立表,再執行其餘語句
				try{
					insertData(db, title, content);
					Cursor cursor = db.rawQuery("select * from news_inf", null);
					inflateList(cursor);
				}catch(SQLiteException se){
					//執行DDL建立數據表
					db.execSQL("create table news_inf(_id integer primary key autoincrement,"
							+" news_title varchar(50),"
							+" news_content varchar(255))");
					//執行insert語句插入數據
					insertData(db, title, content);
					//執行查詢
					Cursor cursor = db.rawQuery("select * from news_inf", null);
					inflateList(cursor);
				}
			}
		});
	}

	
	private void insertData(SQLiteDatabase db
			, String title , String content){
		//執行插入語句
		db.execSQL("insert into news_inf values(null , ? , ?)"
				, new String[]{title , content});
	}
	
	private void inflateList(Cursor cursor){
			//填充SimpleCursorAdapter
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				DBTest.this, R.layout.line, cursor
				, new String[]{"news_title", "news_content"}
				, new int[]{R.id.my_title, R.id.my_content});
		//顯示數據
		listView.setAdapter(adapter);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		//退出程序時關閉SQLiteDatabase
		if(db != null && db.isOpen()){
			db.close();
		}
	}
	
	

}

其中db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "/my.db3", null);用於建立或打開SQLite數據庫。當點擊按鈕的時候,程序會調用insertData方法,向底層數據庫表中插入一行記錄。而後再執行查詢語句,把底層數據表中的記錄查詢出來,並使用ListView將查詢結果(Cursor)顯示出來。 佈局

SimpleCursorAdapter adapter = new SimpleCursorAdapter(
				DBTest.this, R.layout.line, cursor
				, new String[]{"news_title", "news_content"}
				, new int[]{R.id.my_title, R.id.my_content});

以上代碼用於將Cursor封裝成SimpleCursorAdapter,這個SimpleCursorAdapter實現了Adapter接口,能夠做爲ListView的內容適配器。Cursor裏的每一行能夠當成Map處理(以數據列的列名爲key,數據列的值爲value)。SimpleCursorAdapter這裏有5個參數DBTest.this就是當前文件,R.layout.line是line.xml佈局文件,cursor是執行完SQl語句以後,獲取的遊標,第4個參數是from,第5個參數是to,能夠理解爲將數據庫裏的news_title、news_content的字段值賦給line.xml裏面的my_title、my_content。 this

main.xml spa

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".DBTest" >

    <EditText 
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <EditText 
        android:id="@+id/content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:lines="2"/>
    <Button 
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/insert"
        />
	<ListView 
	    android:id="@+id/show"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    />
</LinearLayout>

 

line.xml 翻譯

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
<EditText 
	android:id="@+id/my_title"
	android:layout_width="wrap_content" 
	android:layout_height="wrap_content" 
	android:width="120px"
	/>
<EditText  
	android:id="@+id/my_content"
	android:layout_width="fill_parent" 
	android:layout_height="wrap_content" 
	/>	
</LinearLayout>

 

strings.xml中加入下面一行 ,通常按鈕、TextView的文字都寫在這個文件中,這樣方便維護。好比說須要修改某些字,或者提供不一樣的語言版本的時候,能夠直接在裏面改。固然有些語言開發者不熟悉的時候,能夠直接把整個文件給有能力翻譯的人,這樣很是方便。 調試

<string name="insert">插入</string>

 

總結使用SQLiteDatabase進行數據庫操做的步驟: code

1.獲取SQLiteDatabase對象,它表明了與數據庫的鏈接

2.調用SQLiteDatabase的方法來執行SQL語句

3.操做SQL語句的執行效果,好比用SimpleCursorAdapter封裝Cursor

4.關閉SQLiteDatabase,回收資源

相關文章
相關標籤/搜索