Android菜鸟日记10
SQLite
SQLite
1. 作用:数据库操作
2. 重点:
通过使用SQLiteOpenhelper(数据库助手)来创建数据库。
SQLiteOpenhelper的构造函数有4个参数;
(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
环境、数据库名、不知道、版本号
3. 使用:
1.首先创建一个集成SQLiteOpenhelper的类。覆写
public void onCreate(SQLiteDatabase db) {第一次获得数据对象时调用 一般用于创建数据表}
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {每次版本(version)更新数据库内容时调用}
public void onOpen(SQLiteDatabase db) {每次打开数据库是调用}
2.在activity使用SQLiteOpenhelper时
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3",null,1);
//获得SQLiteOpenhelper对象(自己写的继承SQLiteOpenhelper的对象)。
SQLiteDatabase db=my.getReadableDatabase();
从SQLiteOpenhelper对象获得SQLiteDatabase对象。(第一次获取会调用SQLiteOpenhelper的 onCreate方法)
3.通过获得的SQLiteDatabase对象进行对数据的增删改查。
增
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
//test3为数据库名
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
// ContentValues 类似于hashMap的东西,和bundle一样都是键为String。
//用于存储数据 他的键值为值需要放入的列名ContentValues.put(“列名”,值)
//值需要和生成数据表时的值匹配
c.put("id", i);
i++;
c.put("name", "poolo");
Random r= new Random();
c.put("age", r.nextInt(20));
db.insert("poolo", null, c);
//参数:数据表名、为空时补充的值、ContentValues对象
System.out.println("SQLiteActivity======>insert success");
删
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
//打开的数据库名 test3
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
db.delete("poolo", "id=?", new String[]{"1"});
//参数:待删除的表名、String型(where后面的字句 不过具体指为?)、匹配?的String数组
System.out.println("SQLiteActivity======>delete success");
改
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
c.put("name", "awen");
db.update("poolo", c, "id=?", new String[]{"1"});
//参数:待删除的表名、存放待改的内容的ContentValues 、String型(where后面的字句 不过具体指为?)、匹配?的String数组
System.out.println("SQLiteActivity======>update success");
查
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getReadableDatabase();
Cursor c=db.query("poolo", new String[]{"id","name","age"},null,null, null, null, null);
//这个就复杂了
//数据表名
//需要查询的列名
// String型(where后面的字句 不过具体指为?)
//匹配?的String数组
// String groupBy, 组序
// String having, 不知道
// String orderBy 排序
System.out.println("SQLiteActivity======>update success");
while(c.moveToNext()){
int id= c.getInt(0);
String name =c.getString(1);
int age= c.getInt(2);
System.out.println("sqlquery:id_"+id+",name_"+name+",age_"+age );
补充:
进入android的linux环境的方法[前提模拟器开启状态]
Cmd =>adb shell
可以在对应的包下找到database方法
(具体 cd data cd data cd 包名 cd databases)
到了这里后ls就能看到生成的数据库文件了。
通过 sqlite3 数据库名
就可以进入数据库操作了
.schema
可以查看数据库表的架构
在没有进入数据操作前 可以通过
Rm 文件名
删除文件。
效果:
代码:
SQLiteActivity:
package cfuture.poolo;
import java.util.Random;
import android.app.Activity;
import android.content.ContentValues;
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;
public class SQLiteActivity extends Activity {
private Button createSQLite=null;
private Button updateSQLite=null;
private Button insert=null;
private Button update=null;
private Button select=null;
private Button delete=null;
int i=1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createSQLite=findView(R.id.creatButton);
updateSQLite=findView(R.id.updateSQLite);
insert=findView(R.id.insert);
update=findView(R.id.update);
select=findView(R.id.select);
delete=findView(R.id.delete);
createSQLite.setOnClickListener(new createSQLiteListener());
updateSQLite.setOnClickListener(new updateSQLiteListener());
insert.setOnClickListener(new insert());
update.setOnClickListener(new update());
select.setOnClickListener(new select());
delete.setOnClickListener(new delete());
}
public Button findView(int Rtemp)
{
Button temp=null;
temp=(Button)this.findViewById(Rtemp);
return temp;
}
class createSQLiteListener implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3",null,1);
SQLiteDatabase db=my.getReadableDatabase();
}
}
class updateSQLiteListener implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3",2);
SQLiteDatabase db=my.getReadableDatabase();
}
}
class insert implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
c.put("id", i);
i++;
c.put("name", "poolo");
Random r= new Random();
c.put("age", r.nextInt(20));
db.insert("poolo", null, c);
System.out.println("SQLiteActivity======>insert success");
}
}
class update implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
c.put("name", "awen");
db.update("poolo", c, "id=?", new String[]{"1"});
System.out.println("SQLiteActivity======>update success");
}
}
class select implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getReadableDatabase();
Cursor c=db.query("poolo", new String[]{"id","name","age"},null,null, null, null, null);
System.out.println("SQLiteActivity======>update success");
while(c.moveToNext()){
int id= c.getInt(0);
String name =c.getString(1);
int age= c.getInt(2);
System.out.println("sqlquery:id_"+id+",name_"+name+",age_"+age );
}
}
}
class delete implements OnClickListener
{
@Override
public void onClick(View v) {
MySqliteOpenHelper my=new MySqliteOpenHelper(SQLiteActivity.this,"test3");
SQLiteDatabase db=my.getWritableDatabase();
ContentValues c= new ContentValues();
db.delete("poolo", "id=?", new String[]{"1"});
System.out.println("SQLiteActivity======>delete success");
}
}
}
MySqliteOpenHelper
package cfuture.poolo;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteOpenHelper extends SQLiteOpenHelper {
public static final int version=1;
public MySqliteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context,name,factory,version);
}
public MySqliteOpenHelper(Context context, String name, int version)
{
this(context,name,null,version);
}
public MySqliteOpenHelper(Context context, String name)
{
this(context,name,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table poolo(id int,name varchar(20),age int)");
System.out.println("MySQLiteOpenHelper======>onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
System.out.println("MySQLiteOpenHelper======>onUpgrade");
System.out.println("onUpgrade====="+ arg1 + "|"+ arg2 + "=======");
}
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
System.out.println("MySQLiteOpenHelper======>open");
}
}
布局文件
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"
>
<Button
android:id="@+id/creatButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/createSQLite"/>
<Button
android:id="@+id/updateSQLite"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/updateSQLite"/>
<Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/insert"/>
<Button
android:id="@+id/select"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/select"/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/update"/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</LinearLayout>
2011-8-15
poolo
Android菜鸟日记10
SQLite 数据库