博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android学习之SQLite基础
阅读量:4550 次
发布时间:2019-06-08

本文共 3289 字,大约阅读时间需要 10 分钟。

1、新建MySQLiteHelper类继承自SQLiteOpenHelper 

public class MySQLiteHelper extends SQLiteOpenHelper {

private Context context;
public MySQLiteHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
this.context=context;
}

public static final String createContact = "create table contact("

+ "id integer primary key autoincrement,"
+ "name text,phone text,email text)";

@Override

public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
arg0.execSQL(createContact);

}

@Override

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}

2、实现增加、修改、删除、查询

public class MainActivity extends Activity {

MySQLiteHelper mySQLiteHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//调用getWritableDatabase()方法,检测到当前程序中并没有contact.db数据库,于是会创建该数据库并调用MySQLiteHelper中的onCreate方法创建数据表。

mySQLiteHelper=new MySQLiteHelper(this, "contact.db", null, 1);
mySQLiteHelper.getWritableDatabase();
Button btnInsert=(Button)findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(new OnClickListener() {
//增加
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "aa");
values.put("phone", "13989999099");
db.insert("contact",null, values);
values.clear();
}
});
Button btnUpdate=(Button)findViewById(R.id.btnUpdate);
btnUpdate.setOnClickListener(new OnClickListener() {
//修改
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("email", "itzhb@163.com");
db.update("contact", values, "name=?",new String[]{"aa"});
values.clear();
}
});
Button btnDelete=(Button)findViewById(R.id.btnDelete);
btnDelete.setOnClickListener(new OnClickListener() {
//删除
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();
//db.delete("contact", "id>?",new String[]{"1"});

//直接使用SQL操作数据库

String sqlString="delete from contact where id=(select max(id) from contact)";
db.execSQL(sqlString);
}
});
Button btnQuery=(Button)findViewById(R.id.btnQuery);
btnQuery.setOnClickListener(new OnClickListener() {
//查询
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SQLiteDatabase db=mySQLiteHelper.getWritableDatabase();

//直接使用SQL查询

Cursor cursor=db.rawQuery("select * from contact where id>?", new String[]{"1"});
if(cursor.moveToFirst()){
do {
int id=cursor.getInt(cursor.getColumnIndex("id"));
String nameString=cursor.getString(cursor.getColumnIndex("name"));
String phoneString=cursor.getString(cursor.getColumnIndex("phone"));
String emailString=cursor.getString(cursor.getColumnIndex("email"));
Log.d("contact","id is "+id);
Log.d("contact","name is "+nameString);
Log.d("contact","phone is "+phoneString);
Log.d("contact","email is "+emailString);
} while (cursor.moveToNext());
}
}
});
}

}

另外可以通过sdk\platform-tools目录下的adb工具使用sqlite3命令管理SQLite数据库。

 

转载于:https://www.cnblogs.com/zhouhb/p/4187527.html

你可能感兴趣的文章
Office2007 升级到 office2010
查看>>
Python+Selenium 自动化实现实例-Xpath捕捉元素的几种方法
查看>>
SpringBoot整合Hibernate
查看>>
PPT1 例2
查看>>
extern外部方法使用C#简单例子
查看>>
血液循环结构
查看>>
SQL Server统计数据库中表个数、视图个数、存储过程个数
查看>>
设计模式:观察者模式
查看>>
JVM体系结构之六:堆Heap之1
查看>>
TCP之二:TCP的三次握手与四次分手
查看>>
es的返回数据结构
查看>>
[ActionScript 3.0] as3处理xml的功能和遍历节点
查看>>
linux学习(6)-redhat安装xwindow环境
查看>>
6.28 加法作业
查看>>
CentOS6+nginx+uwsgi+mysql+django1.6.6+python2.6.6
查看>>
【bzoj2829】信用卡凸包 凸包
查看>>
oracle 游标
查看>>
关于拍照那些小事——五一苏行记(三)
查看>>
jquery简单的表单验证充值数量
查看>>
大叔手记(1):使用Visual Studio的查找与替换替代默认的系统搜索
查看>>