SQLite基本操作加数据库分页查询

  • Post author:
  • Post category:其他


先进行简单的数据操作.然后在数据库上面做分页查询的操作,也就是listview分页.看代码

MySQLiteOpenHelper类,继承SQLiteOpenHelper类,是android中操作数据库的辅助类

package zuoyeSQLlite.hall;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {


private static final int dataversion=1;

private  String TableName=””;

/**

* context当前的上下问,DataName数据库名称

*factory游标工厂,version数据库版本,TableName 要创建的表名

*/

public MySQLiteOpenHelper(Context context, String DataName,

CursorFactory factory, int version,String TableName)

{


super(context, DataName, factory, version);

this.TableName=TableName;

}

public MySQLiteOpenHelper(Context context,String DataName,int version,String TableName)

{


this(context, DataName, null, version,TableName);

// TODO Auto-generated constructor stub

}

public MySQLiteOpenHelper(Context context,String DataName,String TableName)

{


this(context, DataName, dataversion,TableName);

// TODO Auto-generated constructor stub

}

//onCreate方法只有在数据库被创建的时候触发

@Override

public void onCreate(SQLiteDatabase db) {


// TODO Auto-generated method stub

String sql=”create table “+TableName+”(_id integer primary key,id text,name text,age text);”;

db.execSQL(sql);

System.out.println(“数据库被创建”);

}

//数据库被打开的时候触发

@Override

public void onOpen(SQLiteDatabase db) {


// TODO Auto-generated method stub

super.onOpen(db);

System.out.println(“数据库被打开”);

}

//数据库本版更新的时候触发

@Override

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {


// TODO Auto-generated method stub

System.out.println(“数据库被更新”);

}

zuoyeSQLlite 类,是一个activity类

//注意.当助手类被new出来的时候,并不会触发其oncreat或者onopen方法,

//只有当取得数据库对象的时候才会触发oncreat或者onopen方法

//如mSqLiteDatabase=mMySQLiteOpenHelper.getWritableDatabase();

//如果数据库存在,则打开数据库触发onopen(),如果数据库不存在,则创建数据库,触发onCreate()

// 数据库操作中几个参数的意思:distinct=”如果多个相同值,只去一个”, table=”表命”, columns=”要查询的列名”,

selection=”查询 条件”,

//    selectionArgs=”条件中用了占位符的参数”, groupBy=”数据分组”, having=”分组后的条件”,

orderBy=”排序方式”, limit=”分页查询”);

具体请看代码:

package zuoyeSQLlite.hall;

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.widget.Button;

import android.widget.EditText;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleCursorAdapter;

import android.widget.TextView;

public class zuoyeSQLlite extends Activity {


private TextView text_pageNo;//显示页码的textview

private Button button_next, button_last;

private ListView listview = null;

private ListAdapter adapter;

private MySQLiteOpenHelper mMySQLiteOpenHelper=null;//助手类

private SQLiteDatabase mSqLiteDatabase=null;//数据库对象

private EditText edit_id,edit_name,edit_age;

private Button button_add,button_update,button_delete,button_query;

private String text_id=””,text_name=””,text_age=””;

private int limit=5;//显示5行数据

private int pageNo=1;//页码

private int pageCount=0;//页面总数

private boolean isfirst=false;

public static final  String TableName=”userinfo1″;

public static final String DataName=”myData”;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mMySQLiteOpenHelper =new MySQLiteOpenHelper(this, DataName, TableName);

findView();// 获得控件对象

isfirst=true;

}

//获取每个控件

private void findView(){


edit_id=(EditText)findViewById(R.id.edit_id);

edit_age=(EditText)findViewById(R.id.edit_age);

edit_name=(EditText)findViewById(R.id.edit_name);

button_add=(Button)findViewById(R.id.button_add);

button_add.setOnClickListener(new MyButtonListener());

button_delete=(Button)findViewById(R.id.button_delete);

button_delete.setOnClickListener(new MyButtonListener());

button_update=(Button)findViewById(R.id.button_update);

button_update.setOnClickListener(new MyButtonListener());

button_query=(Button)findViewById(R.id.button_query);

button_query.setOnClickListener(new MyButtonListener());

button_last = (Button) findViewById(R.id.button_last);

button_last.setOnClickListener(new MyButtonListener());

button_next = (Button) findViewById(R.id.button_next);

button_next.setOnClickListener(new MyButtonListener());

listview = (ListView) findViewById(android.R.id.list);

text_pageNo=(TextView)findViewById(R.id.text_page);

}

//button的监听类

class MyButtonListener implements android.view.View.OnClickListener{

@Override

public void onClick(View v) {


// TODO Auto-generated method stub

switch(v.getId()){


case R.id.button_add:

insert();

break;

case R.id.button_delete:

delete();

break;

case R.id.button_update:

update();

break;

case R.id.button_query:

query(pageNo);

break;

case R.id.button_next:

pageNo++;

if(pageNo>pageCount){


pageNo=1;

}

query(pageNo);

break;

case R.id.button_last:

pageNo–;

if(pageNo<1){


pageNo=pageCount;

}

query(pageNo);

break;

}

}

}

//设置显示的页码

private void setpageNo(int pageNo){


String text=null;

text=pageNo+”/”+pageCount;

text_pageNo.setText(text);

}

//删除数据

private void delete(){


text_id=edit_id.getText().toString().trim();

String sql=”id=”+text_id;

try{


mSqLiteDatabase=mMySQLiteOpenHelper.getWritableDatabase();

mSqLiteDatabase.delete(TableName, sql, null);

}catch(Exception e){


e.getMessage();

System.out.println(“修改出错”);

}

}

//修改数据

private void update(){


text_id=edit_id.getText().toString().trim();

text_age=edit_age.getText().toString().trim();

text_name=edit_name.getText().toString().trim();

String where=”id=”+text_id;

ContentValues values=new ContentValues();

values.put(“id”, text_id);

values.put(“age”, text_age);

values.put(“name”, text_name);

try{


mSqLiteDatabase=mMySQLiteOpenHelper.getWritableDatabase();

mSqLiteDatabase.update(TableName, values, where, null);

}catch(Exception e){


e.getMessage();

System.out.println(“修改出错”);

}

}

//添加数据

private void insert(){


text_id=edit_id.getText().toString().trim();

text_age=edit_age.getText().toString().trim();

text_name=edit_name.getText().toString().trim();

mSqLiteDatabase=mMySQLiteOpenHelper.getWritableDatabase();

ContentValues values=new ContentValues();

values.put(“id”, text_id);

values.put(“age”, text_age);

values.put(“name”, text_name);

try{


mSqLiteDatabase.insert(TableName, null, values);

}catch(Exception e){


e.getMessage();

System.out.println(“插入出错”);

}

}

private void setAdapter(Cursor cur){


/*Cursor cur=null;

cur=getDate();*/

if(cur!=null){


adapter=new SimpleCursorAdapter(zuoyeSQLlite.this, R.layout.user, cur,

new String[]{“_id”,”id”,”name”,”age”}, new int[]{R.id._id,R.id.userid,R.id.username,R.id.userage});

listview.setAdapter(adapter);

}

}

//查询数据

private void query(int pageNo){


mSqLiteDatabase=mMySQLiteOpenHelper.getReadableDatabase();

Cursor cursor=null;

if(isfirst){


cursor= mSqLiteDatabase.query(TableName, null, null,

null,null, null, null, null);

pageCount=cursor.getCount()/limit+1;

isfirst=false;

}

cursor= mSqLiteDatabase.query(TableName, null, null,

null,null, null, null, limit*(pageNo-1)+”,”+limit);//”5,9″,标志从第下标为5的行开始,返回9行数据

System.out.println(limit*(pageNo-1)+”–=–“+limit*pageNo);

s

setpageNo(pageNo);

setAdapter(cursor);

}

}



版权声明:本文为xfks55原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。