SQLite作用及用法

前端之家收集整理的这篇文章主要介绍了SQLite作用及用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlite是android自带的微型功能比较健全的手机数据库,他有一大辅助类sqliteOpenHelper,当我们自己建立一个类必须继承它,并且提供两方面的功能

第一,getReadableDatabase(),getWritableDatabase()可以获得sqliteDatabase对象,通过该对象可以对数据库进行操作;

第二,提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作

下面是实例用法

public class DatabaseHelper extends sqliteOpenHelper {

private static final int VERSION = 1;
//在sqliteOepnHelper的子类当中,必须有该构造函数
public DatabaseHelper(Context context,String name,CursorFactory factory,
int version) {
//必须通过super调用父类当中的构造函数
super(context,name,factory,version);
// TODO Auto-generated constructor stub
}
public DatabaseHelper(Context context,String name){
this(context,VERSION);
}
public DatabaseHelper(Context context,int version){
this(context,null,version);
}

//该函数是在第一次创建数据库的时候执行,实际上是在第一次得到sqliteDatabse对象的时候,才会调用这个方法
@Override
public void onCreate(sqliteDatabase db) {
// TODO Auto-generated method stub
System.out.println("create a Database");
//execsql函数用于执行sql语句
db.execsql("create table user(id int,name varchar(20))");
}

@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
// TODO Auto-generated method stub
System.out.println("update a Database");
}

}
Activity运行程序

package com.example.sqllite;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.sqlite3.db.DatabaseHelper;

public class MainActivity extends Activity {
private Button createDatabase,updateDatabase,insert,update,query,delete;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createDatabase = (Button)findViewById(R.id.createDatabase);
createDatabase.setOnClickListener(new CreateDatabase());

updateDatabase = (Button)findViewById(R.id.updateDatabase);
updateDatabase.setOnClickListener(new UpdateDatabase());

insert = (Button)findViewById(R.id.insert);
insert.setOnClickListener(new Insert());

update = (Button)findViewById(R.id.update);
update.setOnClickListener(new Update());

query = (Button)findViewById(R.id.query);
query.setOnClickListener(new Query());

delete = (Button)findViewById(R.id.delete);
delete.setOnClickListener(new Delete());
}
class CreateDatabase implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建helper对象,传入参数上下文与数据库名字
DatabaseHelper databasehelper = new DatabaseHelper(MainActivity.this,"test_sqlite");
//单有上面的还不能完成创建,需要将数据读出加载到手机数据库当中完成创建数据库操作
sqliteDatabase db = databasehelper.getReadableDatabase();
Toast.makeText(MainActivity.this,"createDatabase",3000).show();
}
}
class UpdateDatabase implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper databasehelper = new DatabaseHelper(MainActivity.this,"test_sqlite",2);
sqliteDatabase db = databasehelper.getReadableDatabase();
Toast.makeText(MainActivity.this,"updateDatabase",3000).show();
}
}
class Insert implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ContentValues values = new ContentValues();
values.put("id",1);
values.put("name","zhangsan");
DatabaseHelper databasehelper = new DatabaseHelper(MainActivity.this,"test_sqlite");
//写入
sqliteDatabase db = databasehelper.getWritableDatabase();
db.insert("user",values);
Toast.makeText(MainActivity.this,"insert",3000).show();
}
}
//更新操作相当于执行sql语句当中的update语句
//UPDATE table_name SET XXCol=XXX WHERE xxCol =xx..就从哪的值设置成什么样
class Update implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper databasehelper = new DatabaseHelper(MainActivity.this,"test_sqlite");
sqliteDatabase db = databasehelper.getWritableDatabase();
ContentValues values = new ContentValues();
//将name列的值改成什么
values.put("name","lisi");
//然后更新user参数是要更新的表名
//第二个是ContentValues对象把name列的值改成lisi
//第三个是where子句?是占位符
//第四个一个元素付值到第一个?
db.update("user",values,"id=?",new String[]{"1"});
Toast.makeText(MainActivity.this,"Update",3000).show();
}
}
class Query implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//先获得继承sqlitehelper类对象
DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this,"test_sqlite");
//然后读取上述类的数据放入到sqlite数据库对象当中
sqliteDatabase db = databaseHelper.getReadableDatabase();
//读取后调用query方法放入到游标对象当中,则让游标指向这个查询数据
Cursor cursor = db.query("user",new String[]{"id","name"},"id",null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Toast.makeText(MainActivity.this,"query",3000).show();
}

}
}
class Delete implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper databaseHelper = new DatabaseHelper(MainActivity.this,"name");
sqliteDatabase db = databaseHelper.getWritableDatabase();
String sql = "delete from test_sqlite where id = 1";
db.execsql(sql);
Toast.makeText(MainActivity.this,"delete",3000);
}
}
}
在命令行中查看数据库命令:

adb shell //进入linux命令行

ls -l 查看目录 cd data cd data cd项目包名(com.example.sqllite)查看建立的数据库名 ,进入数据库,然后再sqlite3 数据库名查看表,然后select * from 表名

这里我在操作的时候出现了问题,insert不进去数据,query也出现问题,问题已经解决了!“id”,insert方法

猜你在找的Sqlite相关文章