Android学习之SQLite创建表和增删改
MySQLiteDemo.java:
import android.app.Activity; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; public class MySQLiteDemo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 父类onCreate() super.setContentView(R.layout.main); // 默认布局管理器 SQLiteOpenHelper helper = new MyDatabaseHelper(this) ; // 定义数据库辅助类 helper.getWritableDatabase() ; // 以修改方式打开数据库 } }
MyDatabaseHelper.java:
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper { // 继承SQLiteOpenHelper类 private static final String DATABASENAME = "test.db" ; // 数据库名称 private static final int DATABASEVERSION = 1 ; // 数据库版本 private static final String TABLENAME = "mytab" ; // 数据表名称 public MyDatabaseHelper(Context context) { // 定义构造 super(context, DATABASENAME, null, DATABASEVERSION); // 调用父类构造 } @Override public void onCreate(SQLiteDatabase db) { // 创建数据表 String sql = "CREATE TABLE " + TABLENAME + " (" + "id INTEGER PRIMARY KEY ," + "name VARCHAR(50) NOT NULL ," + "birthday DATE NOT NULL)"; // SQL语句 db.execSQL(sql) ; // 执行SQL语句 } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "DROP TABLE IF EXISTS " + TABLENAME ; // SQL语句 db.execSQL(sql); // 执行SQL语句 this.onCreate(db); // 创建表 } }
增删改操作:
MySQLiteDemo.java:
import android.app.Activity; import android.database.sqlite.SQLiteOpenHelper; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MySQLiteDemo extends Activity { private SQLiteOpenHelper helper = null ; // 数据库操作 private MytabOperate mtab = null ; // mytab表操作类 private Button insertBut = null ; // 定义按钮 private Button updateBut = null ; // 定义按钮 private Button deleteBut = null ; // 定义按钮 private static int count = 0 ; // 计数统计 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 父类onCreate() super.setContentView(R.layout.main); // 默认布局管理器 this.insertBut = (Button) super.findViewById(R.id.insertBut) ; // 取得组件 this.deleteBut = (Button) super.findViewById(R.id.deleteBut) ; // 取得组件 this.updateBut = (Button) super.findViewById(R.id.updateBut) ; // 取得组件 this.helper = new MyDatabaseHelper(this) ; // 定义数据库辅助类 this.insertBut.setOnClickListener(new InsertOnClickListenerImpl()) ; // 设置监听 this.deleteBut.setOnClickListener(new DeleteOnClickListenerImpl()) ; // 设置监听 this.updateBut.setOnClickListener(new UpdateOnClickListenerImpl()) ; // 设置监听 } private class InsertOnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { MySQLiteDemo.this.mtab = new MytabOperate( MySQLiteDemo.this.helper.getWritableDatabase()); // 取得可更新的数据库操作 MySQLiteDemo.this.mtab.insert("张三" + count ++ , "1979-08-12") ;// 增加数据 } } private class DeleteOnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { MySQLiteDemo.this.mtab = new MytabOperate( MySQLiteDemo.this.helper.getWritableDatabase()); // 取得可更新的数据库操作 MySQLiteDemo.this.mtab.delete(3) ; // 删除数据 } } private class UpdateOnClickListenerImpl implements OnClickListener { @Override public void onClick(View view) { MySQLiteDemo.this.mtab = new MytabOperate( MySQLiteDemo.this.helper.getWritableDatabase()); // 取得可更新的数据库操作 MySQLiteDemo.this.mtab.update(1, "test哈哈", "1981-06-27") ; // 更新已有数据 } } }
MytabOperate.java:
import android.database.sqlite.SQLiteDatabase; public class MytabOperate { private static final String TABLENAME = "mytab" ; // 表名称 private SQLiteDatabase db = null ; // SQLiteDatabase public MytabOperate(SQLiteDatabase db) { // 构造方法 this.db = db ; } public void insert(String name, String birthday) { String sql = "INSERT INTO " + TABLENAME + " (name,birthday) VALUES ('" + name + "','" + birthday + "')"; // SQL语句 this.db.execSQL(sql); // 执行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; // SQL语句 this.db.execSQL(sql); // 执行SQL语句 this.db.close() ; // 关闭数据库操作 } public void delete(int id) { String sql = "DELETE FROM " + TABLENAME + " WHERE id=" + id;// SQL语句 this.db.execSQL(sql) ; // 执行SQL语句 this.db.close() ; // 关闭数据库操作 } }
相关推荐
specialbrian 2020-07-31
loveandroid0 2020-06-18
DAV数据库 2020-06-17
URML 2020-06-13
Dlanguage 2020-06-12
Plant 2020-06-07
疯狂老司机 2020-06-07
chibangyuxun 2020-06-07
sdwylry 2020-06-04
airfling 2020-05-31
Plant 2020-05-31
zbcaicai 2020-05-26
chaochao 2020-05-19
Plant 2020-05-17
小火车 2020-05-14
beibeijia 2020-04-25
Rain 2020-04-16