public class MytabOperate {
private static final String TABLENAME = "mytab"// 表示要 *** 作的数据表名称
private SQLiteDatabase db = null// 数据库 *** 作
public MytabOperate(SQLiteDatabase db) {
this.db = db
}
public void insert(String name,String birthday) {
String sql = "INSERT INTO " + TABLENAME + "(name,birthday) VALUES ('"
+ name + "','" + birthday + "')"
this.db.execSQL(sql)
this.db.close()
}
public void update(int id, String name, String birthday) {
String sql = "UPDATE " + TABLENAME + " SET name='" + name
+ "',birthday='" + birthday + "' WHERE id=" + id
this.db.execSQL(sql)
this.db.close()
}
public void delete(int id) {
String sql = "DELETE FROM " + TABLENAME + " WHERE id=" + id
this.db.execSQL(sql)
this.db.close()
}
}
然后我们定义布局文件三个按钮分别实现三个功能:
<Button
android:id="@+id/insertBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加数据" />
<Button
android:id="@+id/updateBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改数据" />
<Button
android:id="@+id/deleteBut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除数据" />
然后定义Activity方法
public class MainActivity extends Activity {
private Button insertBut = null
private Button updateBut = null
private Button deleteBut = null
private SQLiteOpenHelper helper = null
private MytabOperate mtab = null
private static int count = 0
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
super.setContentView(R.layout.activity_main)
this.helper = new MyDatabaseHelper(this)
this.insertBut = (Button) super.findViewById(R.id.insertBut)
this.updateBut = (Button) super.findViewById(R.id.updateBut)
this.deleteBut = (Button) super.findViewById(R.id.deleteBut)
this.insertBut.setOnClickListener(new InsertOnClickListenerImpl())
this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl())
this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl())
}
private class InsertOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.mtab = new MytabOperate(
MainActivity.this.helper.getWritableDatabase())
MainActivity.this.mtab.insert("zhangyiyi" + count++, "2000-12-21")
}
}
private class UpdateOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.mtab = new MytabOperate(
MainActivity.this.helper.getWritableDatabase())
MainActivity.this.mtab.update(3, "ee", "2321-23-32")
}
}
private class DeleteOnClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
MainActivity.this.mtab = new MytabOperate(
MainActivity.this.helper.getWritableDatabase())
MainActivity.this.mtab.delete(3)
}
}
}
到这里我们就实现了这些功能
怎么查看数据库呢
首先进入
c:\>adb shell
Android把数据都存放在data/data目录下。
我们使用cd命令转到data/data目录下:
cd /data/data
ls 显示所有数据
目录一样有个com.misoo.SQ01文件夹。
cd com.misoo.SQ01/databases 进入com.misoo.SQ01/databases目录
ls 命令你会看到StudDB.db,这就是我们的StudDB.db数据库。
Android中对于SQLite3数据库的 *** 作如下:1、建立数据库
SQLiteDatabasemDatabase
mDatabase
=
openOrCreateDatabase(
"my_sqlite_database.db”,
SQLiteDatabase.CREATE_IF_NECESSARY,
null
)
2、配置属性
mDatabase.setVersion(1)
mDatabase.setLocale(Locate.getDefault())
mDatabase.setLockingEnable(true)
3、建表
CREATETABLE
tb_test
(
idINTEGER
PRIMARYKEY
AUTOINCREMENT,
firstnameTEXT,
lastnameTEXT
)
4、数据库 *** 作
insert(Peoplepeople)用来添加一条数据
queryAllData()用来获取全部数据
queryOneData(long
id)根据id获取一条数据
deleteAllData()用来删除全部数据
deleteOneData(long
id)根据id删除一条数据
updateOneData(long
id
,
People
people)根据id更新一条数据
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)