android之SQLite数据库应用(一)
先上一个小程序,具体的解析介绍会在以后的文章中详细说明,已经凌晨0:40了,我实在是困了,只能把这活移到明天了。今天先把代码粘贴上。
效果图:(向数据库中存入数据,然后再把数据读出来)
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/textView" /> </LinearLayout>
activity的代码
package cn.com.SQLite; import java.util.ArrayList; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.os.Bundle; import android.util.Log; import android.widget.TextView; /** * * @author chenzheng_java * @description SQLite数据库在android中的应用 * @since 2011/03/05 * */ public class SQLiteActivity extends Activity { private String result = "结果:/n"; private String tableName = "chenzheng_Java"; public static final int version_1 = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); run(); TextView textView = (TextView) findViewById(R.id.textView); textView.setText(result); } /** *@description 对数据进行增删改查 */ private void run() { clear(); save(); read(); } /** * 从数据表中读出记录 */ private void read() { MySqliteOpenHandler handler = new MySqliteOpenHandler(this, tableName, null, version_1); SQLiteDatabase database = handler.getWritableDatabase(); Cursor cursor = database.query(tableName, null, null, null, null, null, null); int count = cursor.getCount(); Log.i("通知", "总记录数" + count); int index = cursor.getColumnIndex("name"); Log.i("通知", "nameIndex" + index); int indexOfAge = cursor.getColumnIndex("age"); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Log.i("通知", "here"); String name = cursor.getString(index); result += "/n 姓名:" + name; int age = cursor.getInt(indexOfAge); result += " 年龄:" + age; cursor.moveToNext(); } database.close(); } /** * 保存一些数据到表中 */ private void save() { ArrayList<Beauty> beautyList = getData(); MySqliteOpenHandler handler = new MySqliteOpenHandler( SQLiteActivity.this, tableName, null, version_1); SQLiteDatabase database = handler.getWritableDatabase(); for (Beauty beauty : beautyList) { // 开始事物 database.beginTransaction(); // 以键值对形式存储列名和值的对象 ContentValues contentValues = new ContentValues(); contentValues.put("name", beauty.getName()); contentValues.put("age", beauty.getAge()); long flag = database.insertOrThrow(tableName, "age", contentValues); Log.i("通知", beauty.toString()); if (flag == -1) { Log.i("通知", "insert操作失败"); } else { database.setTransactionSuccessful(); } // 结束事物 database.endTransaction(); } database.close(); } /** * 清除表中原有的记录 */ private void clear() { MySqliteOpenHandler handler = new MySqliteOpenHandler( SQLiteActivity.this, tableName, null, version_1); SQLiteDatabase database = handler.getWritableDatabase(); database.delete(tableName, null, null); database.close(); } /** * * @return 初始化数据 */ private ArrayList<Beauty> getData() { ArrayList<Beauty> beautyList = new ArrayList<Beauty>(); Beauty beauty = null; beauty = new Beauty("小医仙", 23); beautyList.add(beauty); beauty = new Beauty("萧薰儿", 21); beautyList.add(beauty); beauty = new Beauty("杜梅莎女王", 24); beautyList.add(beauty); return beautyList; } /** * @author chenzheng_java * @description 实现我们自己的sqlite的创建及更新帮助类 * @since 2011/03/05 */ private class MySqliteOpenHandler extends SQLiteOpenHelper { public MySqliteOpenHandler(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); Log.i("通知", "MySqliteOpenHandler实例化完毕"); } /** * onCreate方法在系统中不存在用户查询的表时执行,我们在这里可以实现该方法并做一些额外的操作 */ @Override public void onCreate(SQLiteDatabase db) { db.execSQL("drop table if exists " + tableName); db .execSQL("create table if not exists ‘" + tableName + "‘ (id INTEGER primary key,name varchar(20),age INTEGER)"); Log.i("通知", "创建表成功!"); } /** * onUpgrade方法在系统中应用版本发生改变的时候执行 */ @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.i("通知", "版本升级成功!"); } } /** * * @author chenzheng_Java * @description 美人实体类 */ private class Beauty { String name; int age; public Beauty() { } public Beauty(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Beauty [age=" + age + ", name=" + name + "]"; } } }
其他都为默认。运行,便可得到如上结果。
------------------------------------------------------------------------
这里请注意一点,MySqliteOpenHandler该类的构造方法中,第二个参数,实际上代表的是数据库的名字,而并非表名称,这里直接使用了表名进行构建,是为了让代码看的稍微少些,也满足下笔者偷懒的欲望,呵呵。切记 切记
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。