数据库初始化

来源:互联网 发布:北京时时彩计划数据 编辑:程序博客网 时间:2024/04/28 03:33

//创建数据库

package com.example.address_list.Sql;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class Phone_Sqlite extends SQLiteOpenHelper {

    public Phone_Sqlite(Context context) {
        super(context, "address_db", null, 1);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        db.execSQL("create table _phone (_id integer primary key autoincrement,name varchar(20),tel varchar(20))");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

}

//

package com.example.address_list.Dao;

import java.util.ArrayList;

import com.example.address_list.Sql.Phone_Sqlite;
import com.example.address_list.bean.Phone;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class Dao {
    private Phone_Sqlite phone_Sqlite;

    public Dao(Context context) {
        phone_Sqlite = new Phone_Sqlite(context);
    }

    public void add(String name, String tel) {
        SQLiteDatabase writableDatabase = phone_Sqlite.getWritableDatabase();
        writableDatabase.execSQL(
                "insert into _phone (name,tel) values (?,?)",
                new Object[] {  name, tel });
        writableDatabase.close();
    }

    public void delete(String name) {
        SQLiteDatabase writableDatabase = phone_Sqlite.getWritableDatabase();
        writableDatabase.execSQL("delete from _phone where name=?",
                new String[] { name });
    }

    public void update(String newname, String oldname, String tel) {
        SQLiteDatabase writableDatabase = phone_Sqlite.getWritableDatabase();
        writableDatabase.execSQL(
                "update _phone set name=? ,tel=? where name=?",
                new String[] { newname, tel, oldname });
        writableDatabase.close();
    }

    ArrayList<Phone> Plist = new ArrayList<Phone>();

    public ArrayList<Phone> query() {
        Plist.clear();
        SQLiteDatabase readableDatabase = phone_Sqlite.getReadableDatabase();
        Cursor cursor = readableDatabase.rawQuery("select distinct*from _phone",
                new String[] {});
        while (cursor.moveToNext()) {
            //int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String tel = cursor.getString(cursor.getColumnIndex("tel"));
            Plist.add(new Phone( name, tel));
        }
        cursor.close();
        readableDatabase.close();
        return Plist;

    }
}
//查询

contentResolver = getContentResolver();
        list = new ArrayList<Phone>();
        dao = new Dao(MainActivity.this);

        Uri uri_raw = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri uri_data = Uri.parse("content://com.android.contacts/data");
        Cursor cursor = contentResolver.query(uri_raw,
                new String[] { "contact_id" }, null, null, null);
        while (cursor.moveToNext()) {
            String contact_id = cursor.getString(cursor
                    .getColumnIndex("contact_id"));
            if (contact_id == null) {
                continue;
            }
            Cursor query = contentResolver.query(uri_data, null,
                    "raw_contact_id=?", new String[] { contact_id }, null);
            Phone phone = new Phone();
            while (query.moveToNext()) {
                String mimetype = query.getString(query
                        .getColumnIndex("mimetype"));
                String data1 = query.getString(query.getColumnIndex("data1"));
                if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                    phone.setTel(data1);
                } else if ("vnd.android.cursor.item/name".equals(mimetype)) {
                    phone.setName(data1);
                }
            }
            list.add(phone);
            phone = null;
        }
        mybaseAdapter = new MybaseAdapter();
        listView.setAdapter(mybaseAdapter);
        for (Phone phone : list) {
            dao.add(phone.getName(), phone.getTel());
        }

    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        list.clear();
        
        plist = dao.query();
        list.addAll(plist);
        mybaseAdapter.notifyDataSetChanged();
    }

0 0
原创粉丝点击