SQLite_数据存储代码实例
1.sqliets
package com.example.laji;
import android.content.context;
import android.database.sqlite.sqlitedatabase;
import android.database.sqlite.sqliteopenhelper;
public class sql extends sqliteopenhelper{
public sql(context context) {
super(context, "user.db", null, 1);
}
@override
public void oncreate(sqlitedatabase sqlitedatabase) {
sqlitedatabase.execsql("create table user(id integer primary key autoincrement," +
"name text)");
}
@override
public void onupgrade(sqlitedatabase sqlitedatabase, int i, int i1) {
}
}
2.dao_增查
package com.example.laji;
import android.content.contentvalues;
import android.content.context;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import java.util.arraylist;
import java.util.list;
public class dao {
sql sql ;
sqlitedatabase database;
public dao(context context) {
sql = new sql(context);
database = sql.getreadabledatabase();
}
public void add(string name){
contentvalues values = new contentvalues();
values.put("name",name);
database.insert("user",null,values);
}
public list<userbean> select(){
list<userbean> list = new arraylist<>();
userbean userbean;
cursor query = database.query("user", null, null, null, null, null, null);
while (query.movetonext()){
string name = query.getstring(query.getcolumnindex("name"));
userbean = new userbean(name);
list.add(userbean);
}
return list;
}
}
3.userbean
package com.example.laji;
public class userbean {
private string name;
public userbean(string name) {
this.name = name;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
}
上一篇: 一个简单的网上书城的例子(八)
下一篇: 使用Python实现音频双通道分离