android中SQLite的使用总结,用excSQL和rawQuery方法实现一般得增删改查
1:l的内容
[html] view plaincopy
1.<?xml version="1.0" encoding="utf-8"?>
2.<manifest xmlns:android="/apk/res/android"
3.package="cn.itcast.db"
4.android:versionCode="1"
5.android:versionName="1.0">
6.<application android:icon="@drawable/icon" android:label="@string/app_name">
7.<uses-library android:name="st.runner" />
8.<activity android:name=".MainActivity"
9.android:label="@string/app_name">
10.<intent-filter>
11.<action android:name="android.intent.action.MAIN" />
12.<category android:name="android.intent.category.LAUNCHER" />
13.</intent-filter>
14.</activity>
15.
16.</application>
17.
18.<uses-sdk android:minSdkVersion="8" />
19.
20.<instrumentation android:name="st.InstrumentationTestRunner"
21.android:targetPackage="cn.itcast.db"
22.android:label="Test for My App"/>
23.
安卓intent用法24.</manifest>
2:Person类
[java] view plaincopy
1.package cn.itcast.domain;
2.
3.public class Person {
4.
5.private Integer id;
6.private String name;
7.
8.public Person() {
9.
10.}
11.
12.public Integer getId() {
13.return id;
14.}
15.
16.public void setId(Integer id) {
17.this.id = id;
18.}
19.
20.public String getName() {
21.return name;
22.}
23.
24.public void setName(String name) {
25.this.name = name;
26.}
27.
28.@Override
29.public String toString() {
30.return "Person [id=" + id + ", name=" + name + "]";
31.}
32.
33.}
3:DBOpenHelper类,该类继承了SQLiteOpenHelper类
[java] view plaincopy
1.package cn.itcast.service;
2.
3.import&t.Context;
4.import android.database.sqlite.SQLiteDatabase;
5.import android.database.sqlite.SQLiteOpenHelper;
6.import android.database.sqlite.SQLiteDatabase.CursorFactory;
7.
8.public class DBOpenHelper extends SQLiteOpenHelper {
9.
10.private static final String DATABASENAME = "cn.itcast.db";
11.private static final int DATABASEVERSION = 1;
12.
13./*
14.* 构造函数
15.*/
16.public DBOpenHelper(Context context) {
17.super(context, DATABASENAME, null, DATABASEVERSION);
18.}
19.
20./*
21.* 数据库第一次生成时调用该方法,创建一些表或者初始化一些数据
22.* @see android.database.sqlite.SQLiteOpenHelper#onCreate(android.database.sqlite.SQLiteDatabase)
23.*/
24.@Override
25.public void onCreate(SQLiteDatabase db) {
26.db.execSQL("create table person(personid integer primary key autoincrement, name varchar(20))");
27.}
28.
29.@Override
30.public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
31.
32.}
33.
34.}
4:PersonService类
[java] view plaincopy
1.package cn.itcast.service;
2.
3.import java.util.ArrayList;
4.import java.util.List;
5.
6.import&t.Context;
7.import android.database.Cursor;
8.import android.database.sqlite.SQLiteDatabase;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论