数据库初始化
来源:互联网 发布:北京时时彩计划数据 编辑:程序博客网 时间: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();
}
- 数据库初始化
- 数据库初始化
- 初始化数据库
- 数据库初始化
- Ant初始化数据库[实战]
- 手工初始化数据库
- openLDAP的数据库初始化
- 修改数据库初始化参数
- 数据库设置初始化脚本
- 初始化 MySQL 数据库
- 数据库初始化过程
- 数据库初始化脚本
- 设置数据库初始化参数
- Android 初始化本地数据库
- Android sqlite 初始化数据库
- 解析xml初始化数据库
- 数据库初始化参数
- oracle数据库序列初始化
- HTTP/TCP/IP
- softmax——logicstic的泛化
- 用安卓手机控制电脑关机
- java将成为软件发展的规范和希望
- 进程并发与互斥性问题小结
- 数据库初始化
- Shallow heap & Retained heap
- Spark RDD 到 LabelPoint的转换(包含构造临时数据的方法)
- CSDN-markdown编辑器使用教程
- Lintcode 翻转字符串
- 【9-1-1】静态链表和动态链表
- char *a 与char a[] 的区别
- C++ builder提供从MFC到自身支持的过程
- 浅谈决策树c4.5