Android:数据存储之SQLite
Android在运行时集成了SQLite , 所以每个Android应用程序都可以使用SQLite数据库。
我们通过SQLiteDatabase这个类的对象操作SQLite数据库,而且不需要身份验证。
数据库存放的位置:data/<项目文件夹>/databases/
public class MainActivity extends Activity { private Button createDatabase, createTable, selectData, insertData, updateData, deleteDaata; private MySQLiteOpenHelper helper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.demo); // 初始化按钮 createDatabase = (Button) findViewById(R.id.createDatabase); createTable = (Button) findViewById(R.id.createTable); selectData = (Button) findViewById(R.id.selectData); insertData = (Button) findViewById(R.id.insertData); updateData = (Button) findViewById(R.id.updateData); deleteDaata = (Button) findViewById(R.id.deleteDaata); helper = new MySQLiteOpenHelper(MainActivity.this, "mydb.db", null, 1); /* * 创建数据库 */ createDatabase.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase = helper.getWritableDatabase(); Toast.makeText(MainActivity.this, "数据库创建成功", 1000).show(); } }); /* * 创建表 */ createTable.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase = helper.getWritableDatabase(); // 创建表 sqlitedatabase .execSQL("create table student(id INTEGER PRIMARY KEY autoincrement,name text)"); Toast.makeText(MainActivity.this, "表创建成功", 1000).show(); } }); /* * 插入数据 */ insertData.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase = helper.getWritableDatabase(); sqlitedatabase .execSQL("insert into student(name) values (‘tinyphp‘)"); Toast.makeText(MainActivity.this, "插入数据成功", 1000).show(); } }); /* * 更新数据 */ updateData.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase = helper.getWritableDatabase(); sqlitedatabase .execSQL("update student set name=‘monkey‘ where id=‘1‘"); Toast.makeText(MainActivity.this, "数据更新成功", 1000).show(); } }); /* * 查询数据 */ selectData.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase = helper.getReadableDatabase(); //参数:表名、查询的列、查询条件、查询参数、查询结果分组、分组结果限制、排序 Cursor cursor = sqlitedatabase.query("student", new String[] { "id", "name" }, "id=?", new String[] { "1" }, null, null, null); String name=null; while(cursor.moveToNext()){ name =cursor.getString(cursor.getColumnIndex("name")); } Toast.makeText(MainActivity.this, "查询结果name为:"+name, 1000).show(); } }); /* * 删除数据 * */ deleteDaata.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub SQLiteDatabase sqlitedatabase =helper.getWritableDatabase(); sqlitedatabase.delete("student", "id=?", new String[]{"1"}); Toast.makeText(MainActivity.this, "数据删除成功", 1000).show(); } }); } }
相关文章:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。