main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="插入数据" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取数据" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改数据" /> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除数据" /> </LinearLayout>
main.java
package com.hdjc.sqllitedemo; import com.hdjc.sqllitedemo.MainActivity; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { Button btnAdd; Button btnQuery; Button btnUpdate; Button btnDelete; SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 每个应用程序都有独立的Android数据库。不会互相影响。 // 创建一个数据库文件。 db = openOrCreateDatabase("users.db", MODE_PRIVATE, null); // 创建一张表 db.execSQL("create table if not exists tb_user(id integer primary key autoincrement,name text not null,age integer not null,sex text not null)"); btnAdd = (Button) findViewById(R.id.button1); btnQuery = (Button) findViewById(R.id.button2); btnUpdate = (Button) findViewById(R.id.button3); btnDelete = (Button) findViewById(R.id.button4); btnAdd.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 插入数据,使用execsql方式插入数据,缺点在于无法获取返回值类型,插入语句不能省略into db.execSQL("insert into tb_user(name,sex,age) values('李四','男',20)"); db.execSQL("insert into tb_user(name,sex,age) values('王五','女',28)"); db.execSQL("insert into tb_user(name,sex,age) values('赵六','女',30)"); } }); btnQuery.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 执行返回游标的查询 Cursor c = db.rawQuery("select * from tb_user", null); // 循环获取游标中的数据 while (c.moveToNext()) { // 可以通过c.getXXX获取游标中的数据,参数为int类型,列的索引,可以通过c.getColumnIndex根据名称获取列索引,从0开始 int id = c.getInt(0); String name = c.getString(c.getColumnIndex("name")); String sex = c.getString(c.getColumnIndex("sex")); int age = c.getInt(c.getColumnIndex("age")); Log.i("user", "id:" + id + ",name:" + name + ",sex:" + sex + ",age:" + age); } } }); btnUpdate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { db.execSQL("update tb_user set name='张三丰' where id=1"); Toast.makeText(MainActivity.this, "修改成功", 1).show(); } }); btnDelete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //删除语句,不能省略from语句 db.execSQL("delete from tb_user where id=1"); Toast.makeText(MainActivity.this, "删除成功", 1).show(); } }); } }