zoukankan      html  css  js  c++  java
  • 数据库操作

    1.创建数据库。

    <1>.创建继承SQLiteOpenHelper的类,使用该类对数据库进行操作

    <2>.在该类中对数据库进行创建修改结构。

    public class DbOpenHelper extends SQLiteOpenHelper {

             public DbOpenHelper(Context context) {

                       super(context, "database.db", null, 4);

             }

    //首次创建数据库时调用的方法

             public void onCreate(SQLiteDatabase db) {

                       db.execSQL("create table person(personid integer primary key autoincrement,name varchar(10))");

             }

    //数据库版本号更改时执行的方法

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

            //db.execSQL("alter table person add phone varchar(12) null");

                       db.execSQL("alter table person add amount Integer null");

             }

    }

    2.对数据库进行操作

    <1>创建数据库操作类DBOpenHelper

    <2>调用dbopenhelper.getWritableDatabase()对数据库进行操作。

    调用dbopenhelper.getReadableDatabase()对数据库进行查询。

    返回的是SQLiteDatabase对象,调用该对象的方法对数据库进行操作。

    增:

    public void insertdb(Person person){

                       SQLiteDatabase db=dbopenhelper.getWritableDatabase();

                       ContentValues values=new ContentValues();

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

                       values.put("phone", person.getPhone());

                       values.put("amount", person.getAmount());

                       db.insert("person", "name",values);

                                //db.execSQL("insert into person(name,phone) values(?,?)",new Object[]{person.getName(),person.getPhone()});

        }

    删:

    public void deletedb(int personid){

                 SQLiteDatabase db=dbopenhelper.getWritableDatabase();

                 db.delete("person", "personid=?", new String[]{String.valueOf(personid)});

                 //db.execSQL("delete from person where personid=?",new Object[]{personid});

        }

    改:

    public void updatedb(Person person){

                       SQLiteDatabase db=dbopenhelper.getWritableDatabase();

                       ContentValues values=new ContentValues();

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

                       values.put("phone", person.getPhone());

                       values.put("amount", person.getAmount());

                       db.update("person", values,"personid=?", new String[]{String.valueOf(person.getPersonid())});

                       //db.execSQL("update person set name=?,phone=? where personid=?", new Object[]{person.getName(),person.getPhone(),person.getPersonid()});

        }

    查:

    public Person querydb(int personid){

                       SQLiteDatabase db=dbopenhelper.getReadableDatabase();

                       //Cursor cursor=db.rawQuery("select * from person where personid=?", new String[]{String.valueOf(personid)});

                       Cursor cursor=db.query("person", null,"personid=?", new String[]{String.valueOf(personid)}, null, null, null);

                       Person person=null;

                       if(cursor.moveToFirst()){

                                int pid=cursor.getInt(cursor.getColumnIndex("personid"));

                                String name=cursor.getString(cursor.getColumnIndex("name"));

                           String phone=cursor.getString(cursor.getColumnIndex("phone"));

                           int amount=cursor.getInt(cursor.getColumnIndex("amount"));

                           person=new Person(pid,name,phone,amount);

                       }

                       cursor.close();

                       return person;

        }

    3.数据的显示:

    使用ListView显示

    //使用自定义适配器

       private void show2() {

                       List<Person> persons=services.getScrollData(2, 10);

                       personadapter adapter=new personadapter(this, persons, R.layout.item1);

                       listview.setAdapter(adapter);

             }

      //使用SimpleCursorAdapter适配器

             private void show1() {

                       Cursor person_cursor=services.getScrollData_cursor(2, 10);

                       SimpleCursorAdapter adapter=new SimpleCursorAdapter(this, R.layout.item1,person_cursor, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});

                       listview.setAdapter(adapter);

             }

    //使用SimpleAdapter适配器

             private void show() {

                       List<HashMap<String, String>> data=new ArrayList<HashMap<String, String>>();

                       List<Person> list_person=services.getScrollData(2, 10);

                       for(Person person : list_person)

                       {

                                HashMap<String, String> hash_data=new HashMap<String, String>();

                                hash_data.put("name", person.getName());

                                hash_data.put("phone", person.getPhone());

                                hash_data.put("amount","0");

                                data.add(hash_data);

                       }

                       SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.item1, new String[]{"name","phone","amount"}, new int[]{R.id.name,R.id.phone,R.id.amount});

                       listview.setAdapter(adapter);

    }

    //自定义适配器需要实现BaseAdapter

    public class personadapter extends BaseAdapter {

             private List<Person> persons;

             private int resource;

             private LayoutInflater inflater;

             public personadapter(Context context,List<Person> persons,int resource)

             {

                       this.persons =persons;

                       this.resource=resource;

            inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

             }

             public int getCount() {

                       return persons.size();

             }

             public Object getItem(int arg0) {

            return persons.get(arg0);

             }

             public long getItemId(int arg0) {

                       return arg0;

             }

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

                       if(convertView==null){//缓存的内容,如果为第一页则为空,不然为缓存的内容

                                convertView=inflater.inflate(resource, null);

                       }

                 TextView nameView=(TextView)convertView.findViewById(R.id.name);

                       TextView phoneView=(TextView)convertView.findViewById(R.id.phone);

                       TextView amountView=(TextView)convertView.findViewById(R.id.amount);

                       Person person=persons.get(position);

                       nameView.setText(person.getName());

                       phoneView.setText(person.getPhone());

                       amountView.setText(String.valueOf(person.getAmount()));

                 return convertView;

             }

    }

  • 相关阅读:
    [HDU 1254] 推箱子
    [POJ 1321] 棋盘问题
    Ubuntu fcitx CPU占用率很高解决方法
    超简洁git入门
    [LightOJ 1370] Bi-shoe and Phi-shoe(欧拉函数快速筛法)
    [LightOJ 1341] Aladdin and the Flying Carpet (算数基本定理(唯一分解定理))
    seekg()/seekp()与tellg()/tellp()的用法详解
    绝对路径以及相对路径中的斜杠和反斜杠
    TCP滑动窗口
    TCP的三次握手和四次挥手
  • 原文地址:https://www.cnblogs.com/lvfeilong/p/5435dfg.html
Copyright © 2011-2022 走看看