Android继承SQLiteOpenHelper实现

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android继承SQLiteOpenHelper实现相关的知识,希望对你有一定的参考价值。

参考技术A 千锋扣丁学堂android开发为您解答:
1.实现一个继承SQLiteOpenHelper的类
view plaincopy to clipboardprint?
01.import android.content.ContentValues;
02.import android.content.Context;
03.import android.database.Cursor;
04.import android.database.sqlite.SQLiteDatabase;
05.import android.database.sqlite.SQLiteOpenHelper;
06.public class DBHelper extends SQLiteOpenHelper
07.private static final String DB_NAME="coll.db";
08.private static final String TBL_NAME="CollTbl";
09.private static final String CREATE_TBL="create table"
10.+"CollTbl(_id integer primary key autoincrement,name text,url text,desc text)";
11.
12.private SQLiteDatabase db;
13.DBHelper(Context c)
14.super(c,DB_NAME,null,2);
15.
16.@Override 17.public void onCreate(SQLiteDatabase db)
18.this.db=db;
19.db.execSQL(CREATE_TBL);
20.
21.public void insert(ContentValues values)
22.SQLiteDatabase db=getWritableDatabase();
23.db.insert(TBL_NAME,null,values);
24.db.close();
25.
26.public Cursor query()
27.SQLiteDatabase db=getWritableDatabase();
28.Cursor c=db.query(TBL_NAME,null,null,null,null,null,null);
29.return c;
30.
31.public void del(int id)
32.if(db==null)
33.db=getWritableDatabase();
34.db.delete(TBL_NAME,"_id=?",new StringString.valueOf(id));
35.
36.public void close()
37.if(db!=null)
38.db.close();
39.
40.@Override 41.public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
42.
43.
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHelper extends SQLiteOpenHelper
private static final String DB_NAME="coll.db";
private static final String TBL_NAME="CollTbl";
private static final String CREATE_TBL="create table"
+"CollTbl(_id integer primary key autoincrement,name text,url text,desc text)";
private SQLiteDatabase db;
DBHelper(Context c)
super(c,DB_NAME,null,2);

@Override public void onCreate(SQLiteDatabase db)
this.db=db;
db.execSQL(CREATE_TBL);

public void insert(ContentValues values)
SQLiteDatabase db=getWritableDatabase();
db.insert(TBL_NAME,null,values);
db.close();

public Cursor query()
SQLiteDatabase db=getWritableDatabase();
Cursor c=db.query(TBL_NAME,null,null,null,null,null,null);
return c;

public void del(int id)
if(db==null)
db=getWritableDatabase();
db.delete(TBL_NAME,"_id=?",new StringString.valueOf(id));

public void close()
if(db!=null)
db.close();

@Override public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)


2.创建一个录入数据的类
view plaincopy to clipboardprint?
01.import android.app.Activity;
02.import android.content.ContentValues;
03.import android.content.Intent;
04.import android.os.Bundle;
05.import android.view.View;
06.import android.view.View.OnClickListener;
07.import android.widget.Button;
08.import android.widget.EditText;
09.public class AddActivity extends Activity
10.private EditText et1,et2,et3;
11.private Button b1;
12.@Override 13.public void onCreate(Bundle savedInstanceState)
14.super.onCreate(savedInstanceState);
15.setContentView(R.layout.add);
16.this.setTitle("添加收藏信息");
17.et1=(EditText)findViewById(R.id.EditTextName);
18.et2=(EditText)findViewById(R.id.EditTextUrl);
19.et3=(EditText)findViewById(R.id.EditTextDesc);
20.b1=(Button)findViewById(R.id.ButtonAdd);
21.b1.setOnClickListener(new OnClickListener()
22.public void onClick(View v)
23.String name=et1.getText().toString();
24.String url=et2.getText().toString();
25.String desc=et3.getText().toString();
26.ContentValues values=new ContentValues();
27.values.put("name",name);
28.values.put("url",url);
29.values.put("desc",desc);
30.DBHelper helper=new DBHelper(getApplicationContext());
31.helper.insert(values);
32.Intent intent=new Intent(AddActivity.this,
33.QueryActivity.class);
34.startActivity(intent);
35.
36.);
37.
38.
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class AddActivity extends Activity
private EditText et1,et2,et3;
private Button b1;
@Override public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
this.setTitle("添加收藏信息");
et1=(EditText)findViewById(R.id.EditTextName);
et2=(EditText)findViewById(R.id.EditTextUrl);
et3=(EditText)findViewById(R.id.EditTextDesc);
b1=(Button)findViewById(R.id.ButtonAdd);
b1.setOnClickListener(new OnClickListener()
public void onClick(View v)
String name=et1.getText().toString();
String url=et2.getText().toString();
String desc=et3.getText().toString();
ContentValues values=new ContentValues();
values.put("name",name);
values.put("url",url);
values.put("desc",desc);
DBHelper helper=new DBHelper(getApplicationContext());
helper.insert(values);
Intent intent=new Intent(AddActivity.this,
QueryActivity.class);
startActivity(intent);

);

参考技术B 1).创建一个版本为1的“diaryOpenHelper.db”的数据库,
2).同时创建一个 “diary” 表(包含一个_id主键并自增长,topic字符型100
长度, content字符型1000长度)
3).在数据库版本变化时请删除diary表,并重新创建出diary表。本回答被提问者采纳
参考技术C

王立平--SQLite,SQLiteOpenHelper的简单应用

Android平台提供给我们一个数据库辅助类来创建或打开数据库,这个辅助类继承自SQLiteOpenHelper类。在该类的构造器中,调用Context中的方法创建并打开一个指定名称的数据库对象。继承和扩展SQLiteOpenHelper类主要做的工作就是重写下面两个 方法。

public class MySQLiteHelper extends SQLiteOpenHelper {
public MySQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
//数据库首次创建时运行此方法。一般讲创建表等初始化工作放在此处
//execSQL创建表
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
//创建表
String sql="create table if not exists hero_info("+ "id integer primary key,"+ "name varchar,"+ "level integer)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

MainActivity.java

package com.example.sqllite;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; 
import android.graphics.Color;
import android.os.Bundle;
import android.widget.TextView; 
public class MainActivity extends Activity {
private TextView tv;
private MySQLiteHelper h;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }
    public void init(){
     tv=(TextView) findViewById(R.id.tv);
     h=new MySQLiteHelper(this, "my.db", null, 1);
    insertAndUpdateData(h);
    //查询数据
    String result = queryData(h);
    tv.setTextColor(Color.RED);
    tv.setTextSize(20.0f);
    tv.setText("名字\t等级\n"+result);
   
   
    }
    
 
private void insertAndUpdateData(MySQLiteHelper helper) {
// TODO Auto-generated method stub

// 调用getReadableDatabase 方法返回的并不总是仅仅读数据库对象。
// 一般来说该方法和getWriteableDatabase 方法的返回情况同样,
// 仅仅有在数据库仅开放仅仅读权限或磁盘已满时才会返回一个仅仅读的数据库对象。

    SQLiteDatabase db = helper.getWritableDatabase();
    //使用execSQL方法向表中插入数据
    db.execSQL("insert into hero_info(name,level) values(‘bb‘,0)");
    //使用insert方法向表中插入数据
    ContentValues values = new ContentValues();
    values.put("name", "xh");
    values.put("level", 5);
    //调用方法插入数据
    db.insert("hero_info", "id", values);
    //使用update方法更新表中的数据
    //清空ContentValues对象
    values.clear();
    values.put("name", "xh");
    values.put("level", 10);
    //更新xh的level 为10
    db.update("hero_info", values, "level = 5", null);
    //关闭SQLiteDatabase对象
    db.close();

}  
//从数据库中查询数据
public String queryData(MySQLiteHelper myHelper){
String result = "";
//获得数据库对象
SQLiteDatabase db = myHelper.getReadableDatabase();
//查询表中的数据
Cursor cursor = db.query("hero_info", null, null, null, null, null, "id asc");
//获取name列的索引
int nameIndex = cursor.getColumnIndex("name");
//获取level列的索引
int levelIndex = cursor.getColumnIndex("level");
for (cursor.moveToFirst();!(cursor.isAfterLast());cursor.moveToNext()) {
result = result + cursor.getString(nameIndex)+ "\t\t";
result = result + cursor.getInt(levelIndex)+"\n";
}
cursor.close();//关闭结果集
db.close();//关闭数据库对象
return result;
}
@Override
protected void onDestroy() {
SQLiteDatabase db = h.getWritableDatabase();//获取数据库对象
//删除hero_info表中全部的数据 传入1 表示删除全部行------>点击backbutton
db.delete("hero_info", "1", null);
super.onDestroy();
}

技术分享













































































































以上是关于Android继承SQLiteOpenHelper实现的主要内容,如果未能解决你的问题,请参考以下文章

Android SQlite数据库 SQLiteOpenHelper类

Android笔记——SQLiteOpenHelper类

Android SQLiteDatabase帮助类SQLiteOpenHelper的使用

在android中使用sqliteopenhelper这个辅助类时,哪些操作可能生成一个数据库

数据库和表的创建(SQLiteOpenHelper)

王立平--SQLite,SQLiteOpenHelper的简单应用