xml布局
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/showsomething"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/btn_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="创建数据库" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
/>
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="40dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
/>
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="40dp"
/>
<Button
android:id="@+id/btn_insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加用户"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更新"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示用户"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_showall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示all用户"
android:textSize="20sp"
/>
<Button
android:id="@+id/btn_deleteusertable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除用户表"
android:textSize="20sp"
/>
JAVA:
package com.db.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.EditText;
public class DBUtil
{
static SQLiteDatabase sld;
public static void createOrOpenDatabase() throws Exception
{
sld=SQLiteDatabase.openDatabase
(
"/data/data/com.db/dbtest", //数据库所在路径
null, //CursorFactory
SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY //读写、若不存在则创建
);
String sql0="create table if not exists user(username varchar2(20),password varchar2(20))";
sld.execSQL(sql0);
}
public static void closeDatabase() throws Exception
{
try
{
sld.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//获取用户信息
public static List<String> searchuser(String username){
List<String> list=new ArrayList<String>();
try
{
createOrOpenDatabase();
String sql="select * from user where username='"+username+"'";
Cursor cur=sld.rawQuery(sql, new String[]{});
while(cur.moveToNext())
{
list.add(cur.getString(0)); //username
list.add(cur.getString(1)); //password
}
cur.close();
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
//获取所有用户信息-winxiang
public static List searchalluser(){
List list=new ArrayList();
try
{
createOrOpenDatabase();
String sql="select * from user";
Cursor cur=sld.rawQuery(sql, new String[]{});
while(cur.moveToNext())
{
list.add(cur.getString(0)); //username
list.add(cur.getString(1)); //password
}
cur.close();
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
public static void updatetable(String sql)
{
try
{
createOrOpenDatabase();
sld.execSQL(sql);
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
}
//舍弃user表
public static void droptable(){
try
{
String sql="drop table user";
createOrOpenDatabase();
sld.execSQL(sql);
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
Log.d("DB","had deleted table: user->");
}
}