新手SQLite数据库操作(转)

前端之家收集整理的这篇文章主要介绍了新手SQLite数据库操作(转)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

fu.java 文件


package demo.fu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlException;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class fu extends Activity {
/* 全局变量 */
private static String DATABASE_NAME = "ENEST";
private static int DATABASE_VERSION = 2;
public static String TABLE_NAME_ONE = "ONE";
public static String TITLE_ONE = "Title_one";
public static String BODY_ONE = "Body_one";

public static DatabaseHelper mOpenHelper;
private static int Num_0 = 0;
private static int Num_1 = 1;
private static int Num_2 = 2;
public static ArrayList<Map<String,Object>> arraylistmap;
public static Cursor myCursor_one;

public static String mytitle;
public static String mybody;
public static HashMap<String,Object> mymap;
/* 数据库HELPER类 */
static class DatabaseHelper extends sqliteOpenHelper {
public DatabaseHelper(Context context) {

super(context,DATABASE_NAME,null,DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(sqliteDatabase db) {
//新建一个表,以及表内的字段。
//TODO Auto-generated method stub
//String sql = "CREATE TABLE " + TABLE_NAME_ONE + "(" + TITLE_ONE
//+ " text not null," + BODY_ONE + " text not null " + ");";
//db.execsql(sql);
}
@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
// TODO Auto-generated method stub
}
}
/* 创建TABLE ONE */
private void create_one() {
sqliteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "CREATE TABLE " + TABLE_NAME_ONE + "(" + TITLE_ONE
+ " text not null," + BODY_ONE + " text not null " + ");";
try {
db.execsql("DROP TABLE IF EXISTS diary");
db.execsql(sql);
setTitle("Table one create succeed");
} catch (sqlException e) {
setTitle("Table one create fault");
}

Toast.makeText(fu.this,"新建了一个表 one",Toast.LENGTH_LONG).show();
}

/* 往TABLE ONE 中插入预定数据 */
private void insertTable_one() {
sqliteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ","
+ BODY_ONE + ") values('" + Num_0 + "','[one]this is 0');";
db.execsql(sql);
Log.e("table one ","num 0 has insert");
sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ","
+ BODY_ONE + ") values('" + Num_1 + "','[one]this is 1');";
db.execsql(sql);
Log.e("table one ","num 1 has insert");
sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ","
+ BODY_ONE + ") values('" + Num_2 + "','[one]this is 2');";
db.execsql(sql);
Log.e("table one ","num 2 has insert");
setTitle("Table one insert succeed");

Toast.makeText(fu.this,"插入了数据撒~~~",Toast.LENGTH_LONG).show();
}
/* 往TABLE TWO 中插入预定数据 */

/* 删除TABLE ONE */
private void dropTable_one() {
sqliteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "drop table " + TABLE_NAME_ONE;
try {
db.execsql(sql);
setTitle("DB one delete succeed:" + sql);
} catch (sqlException e) {
setTitle("DB one delete falut");
}
Toast.makeText(fu.this,"删除了表。。。",Toast.LENGTH_LONG).show();
}

/* 显示出TABLE ONE 跳转sqlVIEWLIST.JAVA */
private void show_one() {
sqliteDatabase db = mOpenHelper.getWritableDatabase();
myCursor_one = db.rawQuery("SELECT * FROM ONE",null);
int first = myCursor_one.getColumnIndex(TITLE_ONE);
int second = myCursor_one.getColumnIndex(BODY_ONE);
arraylistmap.clear();
if (myCursor_one.moveToFirst()) {
do {
mytitle = myCursor_one.getString(first);
mybody = myCursor_one.getString(second);
mymap = new HashMap<String,Object>();
mymap.put(TITLE_ONE,mytitle);
mymap.put(BODY_ONE,mybody);
arraylistmap.add(mymap);
} while (myCursor_one.moveToNext());
}
myCursor_one.close();

ListView listView = new ListView(this);
/*
* ADAPTER将ARRAYLISTMAP中的数据以COPY_MAIN的形式输出,TITLE_ONE数据对应LIST_TITLE,
* BODY_ONE数据对应到LIST_BODY
*/
SimpleAdapter adapter = new SimpleAdapter(this,
fu.arraylistmap,R.layout.copy_main,new String[] {
fu.TITLE_ONE,fu.BODY_ONE },new int[] {
R.id.list_title,R.id.list_body });
listView.setAdapter(adapter);
/* 读取游标MYCURSOR_ONE读到的数据条目数,设置为TITLE */
int title = fu.myCursor_one.getCount();
setTitle("Count of Table One: " + title);
setContentView(listView);


}


/*全局变量的初始化 按钮的响应 以及设置显示*/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mOpenHelper = new DatabaseHelper(this);
arraylistmap = new ArrayList<Map<String,Object>>();

Button button1 = (Button) findViewById(R.id.button01);
button1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
create_one();
}
});
Button button2 = (Button) findViewById(R.id.button02);
button2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dropTable_one();
}
});
Button button3 = (Button) findViewById(R.id.button03);
button3.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
insertTable_one();
}
});
Button button4 = (Button) findViewById(R.id.button04);
button4.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
show_one();

}
});

}
}

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="fill_parent"
android:orientation="horizontal"
android:layout_height="80dip">
<Button android:id="@+id/button01" android:text="create one" android:layout_height="80dip" android:layout_width="80dip"></Button>
<Button android:id="@+id/button02" android:text="drop the one" android:layout_height="80dip" android:layout_width="80dip"></Button>
<Button android:id="@+id/button03" android:text="insert one" android:layout_height="80dip" android:layout_width="80dip"></Button>
<Button android:id="@+id/button04" android:text="show one" android:layout_height="80dip" android:layout_width="80dip"></Button>
</LinearLayout>

</LinearLayout>

copy_main.xml文件


<?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="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/list_title"
android:layout_height="wrap_content" android:layout_width="200dip"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/list_body"
android:layout_height="wrap_content" android:layout_width="200dip"
/>
</LinearLayout>

图一 图二 图三 图四

2010-11-16 15:52 上传
下载附件 (11.14 KB)

猜你在找的Sqlite相关文章