SKY外语计算机学习
标题:
安卓数据库SQLlite操作
[打印本页]
作者:
rogan
时间:
2013-5-18 12:11
标题:
安卓数据库SQLlite操作
package com.yarin.android.Examples_06_05;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class Activity01 extends Activity
{
private static int miCount = 0;
/* 数据库对象 */
private SQLiteDatabase mSQLiteDatabase = null;
/* 创建表的sql语句 */
private final static String CREATE_TABLE = "CREATE TABLE table1 ( _id INTEGER PRIMARY KEY, num INTERGER, data TEXT)";
/* 线性布局 */
LinearLayout m_LinearLayout = null;
/* 列表视图-显示数据库中的数据 */
ListView m_ListView = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
/* 创建LinearLayout布局对象 */
m_LinearLayout = new LinearLayout(this);
/* 设置布局LinearLayout的属性 */
m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);
/* 创建ListView对象 */
m_ListView = new ListView(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
m_ListView.setBackgroundColor(Color.BLACK);
Button m_addButton = new Button(this);
m_addButton.setText("add");
m_addButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
AddData();
}
});
Button m_updataButton = new Button(this);
m_updataButton.setText("edit");
m_updataButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
UpData();
}
});
Button m_deleteButton = new Button(this);
m_deleteButton.setText("delete");
m_deleteButton.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
DeleteData();
}
});
/* 添加m_ListView到m_LinearLayout布局 */
m_LinearLayout.addView(m_addButton);
m_LinearLayout.addView(m_updataButton);
m_LinearLayout.addView(m_deleteButton);
m_LinearLayout.addView(m_ListView, param);
/* 设置显示m_LinearLayout布局 */
setContentView(m_LinearLayout);
// 打开已经存在的数据库
mSQLiteDatabase = this.openOrCreateDatabase("Examples_06_05.db", MODE_PRIVATE, null);
// 获取数据库Phones的Cursor
try
{
/* 在数据库mSQLiteDatabase中创建一个表 */
mSQLiteDatabase.execSQL(CREATE_TABLE);
}
catch (Exception e)
{
UpdataAdapter();
}
}
public void DeleteDataBase()
{
this.deleteDatabase("Examples_06_05.db");
this.finish();
}
public void DeleteTable()
{
mSQLiteDatabase.execSQL("DROP TABLE table1");
this.finish();
}
public void UpData()
{
String sqlstr = "update table1 set"
+ " num=888, data='修改的数据888' where num="+Integer.toString(miCount-1);
mSQLiteDatabase.execSQL(sqlstr);
UpdataAdapter();
}
public void AddData()
{
String sqlstr = "INSERT into table1"
+ " (num, data) values ('"+miCount+"','yy4350数据"+miCount+"')";
mSQLiteDatabase.execSQL(sqlstr);
miCount++;
UpdataAdapter();
}
public void DeleteData()
{
String sqlstr = "DELETE FROM table1 " +
" WHERE _id=" + Integer.toString(miCount);
mSQLiteDatabase.execSQL(sqlstr);
miCount--;
if (miCount < 0)
{
miCount = 0;
}
UpdataAdapter();
}
/* 更行试图显示 */
public void UpdataAdapter()
{
// 获取数据库Phones的Cursor
Cursor cur = mSQLiteDatabase.query("table1", new String[] { "_id", "num", "data" }, null, null, null, null, null);
miCount = cur.getCount();
if (cur != null && cur.getCount() >= 0)
{
// ListAdapter是ListView和后台数据的桥梁
ListAdapter adapter = new SimpleCursorAdapter(this,
// 定义List中每一行的显示模板
// 表示每一行包含两个数据项
android.R.layout.simple_list_item_2,
// 数据库的Cursor对象
cur,
// 从数据库的TABLE_NUM和TABLE_DATA两列中取数据
new String[] { "num", "data" },
// 与NAME和NUMBER对应的Views
new int[] { android.R.id.text1, android.R.id.text2 });
/* 将adapter添加到m_ListView中 */
m_ListView.setAdapter(adapter);
}
}
/* 按键事件处理 */
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
/* 退出时,不要忘记关闭 */
mSQLiteDatabase.close();
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
复制代码
欢迎光临 SKY外语计算机学习 (http://join.skywj.com/)
Powered by Discuz! X2.5