大体步骤:
创建sqliteDatabase对象
通过重写sqliteHelper类,执行增删减操作
直接执行查询操作(返回值为游标)
调用moveToXX()方法对游标直接找到相关数据,返回布尔值(游标可滚动,不需要While())
实例代码:
重写sqliteHelper类
<span style="font-size:14px;">package com.study.entity; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; public class Dao extends sqliteOpenHelper { public Dao(Context context) { super(context,"db_con",null,1); // TODO Auto-generated constructor stub } @Override public void onCreate(sqliteDatabase sdb) { // TODO Auto-generated method stub sdb.execsql("CREATE TABLE t_contact(_id intger primary key autoincrement,name varchar(50),phone varchar(50) "); sdb.execsql("insert into t_contact(name,phone)values('williammu','18297927880')"); } @Override public void onUpgrade(sqliteDatabase sdb,int oldVersion,int newVersion) { // TODO Auto-generated method stub // if (oldVersion == 1 && newVersion == 2) { // // } } /** * 得到所有联系人 * * @return Cursor */ public Cursor getAll() { sqliteDatabase sdb = getReadableDatabase(); Cursor cur = sdb.rawQuery("SELECT * FROM t_contact",null); return cur; } /** * 增加联系人 */ public void insertContacts() { Contact contact=new Contact(); sqliteDatabase sdb = getWritableDatabase(); sdb.execsql("INSERT INTO t_contact (name,phone) VALUES ('"+contact.getName()+"','"+contact.getPhone()+"')"); } } </span>谢谢。 原文链接:https://www.f2er.com/sqlite/199863.html