SQLite数据库增加修改删除以及如何查看(7)
按着上节课内容来,SQLite数据库的增加修改删除功能实现
首先mytab操作类,就是处理增加修改删除功能
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 显示所有数据
如果你看过高焕堂老师的<<应用框架原理与程序设计36技>>,并亲自试验过
"9.1#21:SQLite基本操作"章节,你会看到像我目录一样有个com.misoo.SQ01文件夹。
cd com.misoo.SQ01/databases 进入com.misoo.SQ01/databases目录
ls 命令你会看到StudDB.db,这就是我们的StudDB.db数据库。
相关推荐
lbyd0 2020-11-17
sushuanglei 2020-11-12
腾讯soso团队 2020-11-06
gaobudong 2020-11-04
yangkang 2020-11-09
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30