android 43 SQLite数据库

时间:2022-04-15 23:10:08

SQLite数据库很小,占用内存只有几百K,安卓和IOS都是用的SQLite数据库。

android 43  SQLite数据库

android 43  SQLite数据库

android 43  SQLite数据库

android 43  SQLite数据库

android 43  SQLite数据库

android 43  SQLite数据库

android 43  SQLite数据库

页面:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btnInsertData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加数据" />
<Button
android:id="@+id/btnDeleteData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据" />
<Button
android:id="@+id/btnUpdateData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改数据" />
<Button
android:id="@+id/btnQueryData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查询数据" /> </LinearLayout>

java

package com.sxt.day06_09;

import java.util.ArrayList;
import java.util.List; import com.sxt.day06_09.entity.StudentBean; import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener; public class MainActivity extends Activity { static final String DB_NAME="students.db";
static final String TABLE_NAME="student";
static final String ID="_id";//只能写下划线id
static final String NAME="name";
static final String SEX="sex";
static final String BIRTHDAY="birthday";
static final String HEIGHT="height"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
creatDataBase();//创建数据库
setListener();
} private void setListener() {
setInsertClickListener();
setDeleteClickListener();
setQueryClickListener();
} private void setQueryClickListener() {
findViewById(R.id.btnQueryData).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
String sql="select * from "+TABLE_NAME+" where "+NAME+" like ?";
String sql1="select * from "+TABLE_NAME;
Cursor c = db.rawQuery(sql, new String[]{"'%菲%'"});
Cursor c = db.rawQuery(sql, null);//查询只能用rawQuery,返回游标就是查询结果集
List<StudentBean> students=new ArrayList<StudentBean>();
boolean flag = c.moveToFirst();
while(c.moveToNext()){//遍历每一行
int id=c.getInt(c.getColumnIndex(ID));//获取一行的id列
String name=c.getString(c.getColumnIndex(NAME));
String sex=c.getString(c.getColumnIndex(SEX));
String birthday=c.getString(c.getColumnIndex(BIRTHDAY));
double height=c.getDouble(c.getColumnIndex(HEIGHT));
StudentBean bean=new StudentBean(id, name, sex, birthday, height);
students.add(bean);
}
for(StudentBean bean:students){
Log.i("main",bean.toString());
}
c.close();//释放资源
}
});
} private void setDeleteClickListener() {
findViewById(R.id.btnDeleteData).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
String sql="delete from "+TABLE_NAME+" where "+NAME+" like '%菲%'";
db.execSQL(sql);
}
});
} private void setInsertClickListener() {
findViewById(R.id.btnInsertData).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db=openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);
insertData(db);
//数据库路径为:data/data/com.ityw.exer15_1/databases/students.db
}
});
} //创建数据库
private void creatDataBase() {
SQLiteDatabase db = openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null);//创建数据库
String sql="create table if not exists "+TABLE_NAME+"("
+ID+" integer primary key autoincrement,"
+NAME+" varchar(50),"
+SEX+" varchar(2),"
+BIRTHDAY+" datetext,"
+HEIGHT+" real)";
db.execSQL(sql);
} private void insertData(SQLiteDatabase db) {
String sql="insert into "+TABLE_NAME+"("+
NAME+","+SEX+","+BIRTHDAY+","+HEIGHT+")values("+
"'张飞','男','1990-5-5',1.99)";
db.execSQL(sql);
sql="insert into "+TABLE_NAME+"("+
NAME+","+SEX+","+BIRTHDAY+","+HEIGHT+")values("+
"'王菲','女','1990-8-5',1.69)";
db.execSQL(sql);
sql="insert into "+TABLE_NAME+"("+
NAME+","+SEX+","+BIRTHDAY+","+HEIGHT+")values("+
"'刘亦菲','女','1991-8-5',1.71)";
db.execSQL(sql);
} }