SQLiteOpenHelper数据库操作

发现之前写了这么多都没有涉及数据库的内容,但是数据库又是灰常的重要,所以这里就写一下安卓利用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>
文章导航