發現之前寫了這么多都沒有涉及數據庫的內容,但是數據庫又是灰常的重要,所以這里就寫一下安卓利用SQLiteOpenHelper進行數據庫操作了。
數據庫操作一般的思路是用一個類繼承SQLiteOpenHelper,然后就可以各種增刪改查了。必須要注意的是SQLiteDatabase和Cursor不用要close掉,不然會內存泄露。
提一下的是,設A繼承了SQLiteOpenHelper,那么A的onCreate函數只有在數據庫第一次創建的時候才會調用。
下面這個也不算是應用吧,只是輸一句話進數據庫然后顯示出最近的兩句而已。下面是代碼:
~~~
package com.example.mytest;
import android.app.Activity;
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;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText ed;
private TextView tv1;
private TextView tv2;
private Button bt;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed = (EditText) findViewById(R.id.ed);
tv1 = (TextView) findViewById(R.id.tv1);
tv2 = (TextView) findViewById(R.id.tv2);
bt = (Button) findViewById(R.id.bt);
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this);
db = helper.getWritableDatabase();
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String t = ed.getText().toString();
db.execSQL("insert into text (str) values (?)", new Object[]{t});
Cursor cursor = db.rawQuery("select * from text", null);
if (cursor.moveToLast()) {
String ans = cursor.getString(1);
tv1.setText(ans);
}
if (cursor.moveToPrevious()) {
String ans = cursor.getString(1);
tv2.setText(ans);
}
cursor.close();
}
});
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
db.close();
}
}
~~~
~~~
package com.example.mytest;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySQLiteOpenHelper extends SQLiteOpenHelper{
public MySQLiteOpenHelper(Context context) {
super(context, "text.db", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table text(id integer primary key autoincrement, str varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
~~~
~~~
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="matrix"
/>
<EditText
android:id="@+id/ed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
/>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="tv1"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:layout_marginTop="30dp"
android:text="tv2"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv2"
android:layout_marginTop="30dp"
android:layout_centerHorizontal="true"
android:text="保存"
/>
</RelativeLayout>
~~~
- 前言
- 安卓ListView一個簡單代碼的注釋和探討
- 安卓wifi熱點編程代碼的若干注釋
- 安卓距離傳感器編程
- 簡單的ScrollView
- 簡單的ListView
- 簡單的ArrayAdapter
- AsyncTask的初步學習
- AsyncTask再度學習
- Handler初步學習
- 開啟系統Activity
- 隱式調用Activity
- Activity的生命周期
- startActivityForResult的初步學習
- 多點觸控拉伸圖片
- LruCache圖片緩存技術
- SQLiteOpenHelper數據庫操作
- 短信攔截器
- 簡單的Notification
- ListView優化以及checkbox狀態問題
- 安卓多線程下載
- 安卓JSON解析初步探討
- 選項卡樣式的fragment
- DrawerLayout實現簡單的側滑功能
- 安卓軟引用解決圖片OOM問題
- 安卓隱式Intent啟動Activity和BroadcastReceiver若干注意點
- Dialog學習筆記
- ActionBar使用