android 移动开发 SQLiteOpenHelper

SQLiteOpenHelper一个助手类用来管理数据库   如创建和版本更新。

1.创建一个子类实现onCreate(SQLiteDatabase)    onUpgrade(SQLiteDatabase,int,int)方法

         onCreate(SQLiteDatabase)   初次创建数据库时调用,可以在此方法中初始化表数据。

         onUpgrade(SQLiteDatabase,int,int)当数据库更新时调用此方法,如被改变的表需要插入列  删除列  删除表,表重命名等操作。这些操作要相当小心,所以要放在事务中执行。

api给我们的标准事务范例:

 db.beginTransaction();

                  try {

                    ...

                   db.setTransactionSuccessful();

                  } finally {

                   db.endTransaction();

                  }


一具体操作SQLiteOpenHelper如下

importandroid.content.Context;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteDatabase.CursorFactory;

importandroid.database.sqlite.SQLiteOpenHelper;

 

public class SqlHelperextends SQLiteOpenHelper{

public Stringsql="create table if not exists "+Attribute.DB_TABLE_NAME+" (_idinteger primary key autoincrement , name text , download integer,classnametext)";

        public SqlHelper(Context context) {

               super(context, Attribute.DB_NAME, null,Attribute.DB_VERSION);

        }

 

        @Override

        public void onCreate(SQLiteDatabase db) {

               // TODO Auto-generated method stub

               db.execSQL(sql);

        }

 

        @Override

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

               // TODO Auto-generated method stub

 

        }

 

}



public class DBUtil {

        private SqlHelper helper;

        private SQLiteDatabase sd;

 

        public DBUtil(Context context) {

               helper = new SqlHelper(context);

               sd = helper.getReadableDatabase();

        }

 

        // 插入多条数据

        public void insertUser(List<User> list) {

               for (User u : list) {

                       ContentValues values = new ContentValues();

                       values.put("classname",u.getClassname());

                       values.put("download",u.getDownload());

                       values.put("name", u.getName());

                       sd.insert(Attribute.DB_TABLE_NAME, null,values);

               }

        }

 

        // 查询所有用户

        public List<User> selectall() {

               List<User> list = new ArrayList<User>();

               Cursor cs = sd

                               .rawQuery("select * from " +Attribute.DB_TABLE_NAME, null);

               while (cs.moveToNext()) {

                       String name = cs.getString(1);

                       int down = cs.getInt(2);

                       String bookname = cs.getString(3);

                       User user = new User(name, down, bookname);

                       list.add(user);

               }

               return list;

        }

 

        // 插入一条user信息

        public void adduser(User user) {

               ContentValues values = new ContentValues();

               values.put("classname",user.getClassname());

               values.put("download", user.getDownload());

               values.put("name", user.getName());

               sd.insert(Attribute.DB_TABLE_NAME, null, values);

        }

}





public class User {

          private String name;

          private int download;

          private String classname;

 

          public String getName() {

                    return name;

          }

 

          public void setName(String name) {

                    this.name = name;

          }

 

          public int getDownload() {

                    return download;

          }

 

          public void setDownload(int download){

                    this.download = download;

          }

 

          public String getClassname() {

                    return classname;

          }

 

          public void setClassname(Stringclassname) {

                    this.classname = classname;

          }

 

          public User(String name, int download,String classname) {

                    super();

                    this.name = name;

                    this.download = download;

                    this.classname = classname;

          }

 

}



public classAttribute {

public static finalString DB_NAME="db";

public static finalString DB_TABLE_NAME="user";

public static finalint DB_VERSION=1;

}



importjava.util.List;

 

importandroid.content.Context;

import android.view.LayoutInflater;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.widget.BaseAdapter;

importandroid.widget.TextView;

 

importcom.bawei.zhangpengfei.R;

importcom.bawei.zhangpengfei.Vo.User;

 

public class   myAdapter extends BaseAdapter {

          private List<User> list;

          private Context con;

 

          public List<User> getList() {

                    return list;

          }

 

          public void setList(List<User>list) {

                    this.list = list;

          }

 

          public myAdapter(List<User>list, Context context) {

                    super();

                    this.list = list;

                    this.con = context;

          }

 

          @Override

          public int getCount() {

                    // TODO Auto-generatedmethod stub

                    return list.size();

          }

 

          @Override

          public Object getItem(int position) {

                    // TODO Auto-generatedmethod stub

                    return list.get(position);

          }

 

          @Override

          public long getItemId(int position) {

                    // TODO Auto-generatedmethod stub

                    return position;

          }

          @Override

          public View getView(int position, ViewconvertView, ViewGroup parent) {

                    // TODO Auto-generatedmethod stub

                    System.out.println("执行");

                    adapterhelper help = null;

                    if(convertView==null){

                    help=new adapterhelper();

                               convertView =LayoutInflater.from(con).inflate(

                                                   R.layout.list_item,null);

                               help.item_author= (TextView) convertView

                                                   .findViewById(R.id.item_author);

                               help.item_calssname= (TextView) convertView

                                                   .findViewById(R.id.item_className);

                               help.item_download= (TextView) convertView

                                                   .findViewById(R.id.item_download);

                               convertView.setTag(help);

                    }

                               help=(adapterhelper)convertView.getTag();

                    help.item_author.setText(list.get(position).getName());

                    help.item_calssname.setText(list.get(position).getClassname());

                    help.item_download.setText(list.get(position).getDownload()+"");

 

                    return convertView;

          }

 

          static class adapterhelper {

                     TextView item_calssname, item_author,item_download;

          }

 

}



importjava.util.List;

 

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.widget.ListView;

 

importcom.bawei.zhangpengfei.Util.DBUtil;

importcom.bawei.zhangpengfei.Vo.ListUser;

importcom.bawei.zhangpengfei.Vo.User;

 

public classMainListView extends Activity {

          private ListView listview;

          private List<User> addlist;

          private myAdapter adapter;

          private DBUtil util;

 

          @Override

          protected void onCreate(BundlesavedInstanceState) {

                    super.onCreate(savedInstanceState);

                    setContentView(R.layout.item_listview);

                    listview = (ListView)findViewById(R.id.listView1);

                    // 添加数据

//                  addlist = ListUser.getList();

//                  util = newDBUtil(getApplicationContext());

//                  util.insertUser(addlist);

 

                    // 显示数据库里的信息

                    List<User> listAdapter= util.selectall();

                    for (User u : listAdapter) {

                               System.out.println(u.getName());

                    }

                    adapter = newmyAdapter(listAdapter, MainListView.this);

                    listview.setAdapter(adapter);

          }

}



importjava.util.ArrayList;

importjava.util.List;

 

public class ListUser{

 

          public static List<User>getList(){

                    List<User> list=newArrayList<User>();

                    User user=new User("山鸡",581, "出轨36绝技");

                    User user1=new User("大飞",385, "长毛大傻吊");

                    User user2=new User("陈浩南",788, "小结巴");

                    list.add(user1);

                    list.add(user2);

                    list.add(user);

                    return list;

          }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值