首页 技术 正文
技术 2022年11月23日
0 收藏 953 点赞 3,874 浏览 2977 个字

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.zhankr.net/140989.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:7,142
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:4,476
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:567 阅读:5,291
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:5,163
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:6,335
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:3,874