Ⅰ android 如何连接数据库
这种方式通常连接一个外部的数据库,第一个参数就是数据库文件,这个数据库不是当前项目中生成的,通常放在项目的Assets目录下,当然也可以在手机内,如上面参数那个目录,前提是那个文件存在且你的程序有访问权限。
另一种使用数据库的方式是,自己创建数据库并创建相应的数据库表,参考下面的代码:
public class DatabaseHelper extends SQLiteOpenHelper {
//构造,调用父类构造,数据库名字,版本号(传入更大的版本号可以让数据库升级,onUpgrade被调用)
public DatabaseHelper(Context context) {
super(context, DatabaseConstant.DATABASE_NAME, null, DatabaseConstant.DATABASE_VERSION);
}
//数据库创建时调用,里面执行表创建语句.
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createVoucherTable());
}
//数据库升级时调用,先删除旧表,在调用onCreate创建表.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DatabaseConstant.TABLE_NAME);
onCreate(db);
}
//生成 创建表的SQL语句
private String createVoucherTable() {
StringBuffer sb = new StringBuffer();
sb.append(" CREATE TABLE ").append(DatabaseConstant.TABLE_NAME).append("( ").append(“ID”)
.append(" TEXT PRIMARY KEY, ")
.append(“USER_ID”).append(" INTEGER, ").append(“SMS_CONTENT”).append(" TEXT ) ");
return sb.toString();
}
} 继承SQLiteOpenHelper并实现里面的方法.
之后:
//得到数据库助手类
helper
=
new
DatabaseHelper(context);
//通过助手类,打开一个可读写的数据库连接
SQLiteDatabase
database
=
helper.getReadableDatabase();
//查询表中所有记录
database.query(DatabaseConstant.TABLE_NAME,
null,
null,
null,
null,
null,
null);
Ⅱ android 怎么往数据库里面添加数据
参考如下内容:
你通过getText()方法首先得到输入的值,然后调用数据库的插版入方法 db.insert();插入到权数据库中就行 就想这样
EditText et ;
String num = et.getText().toString();
public void addData(String num) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("num", num);
db.insert("表名", null, values);
}
当你调用这个 addData()方法时就会向数据库中插入数据了
Ⅲ 有关于安卓数据库的db.execSQL(sql);里面写的qdl到底代表什么尤其是primary后面的内容
//定义一条sql语句
String sql="create table persons(_id integer primary key autoincrement,name text)";
//执行sql语句
db.execSQL(sql);
那条sql语句是用来创建一张表的。
Ⅳ android sql数据库查询问题
不要用query方法,直接用sql语句查询
select * from notes where _id = "10"
Ⅳ android数据库怎么建立
建立数据库
public class WeatherDataBaseHelper extends SQLiteOpenHelper {
private static final String TAG = "pin";
// private static final String CREAT_TABLE_SQL =
// "create table IPTV(_id integer primary key autoincrement,"
// +"_name String not null, _path String not null)";
// private static final String CREAT_TABLE_SQL = "create table " +
// DataTools.TABLE_NAME + "("
// + DataTools._ID + " integer autoincrement,"
// + DataTools.NAME + " String not null,"
// + DataTools.PATH + " String not null,"
// + DataTools.HASHCODE + " integer "
// + "primary key(" + DataTools._ID + "," + DataTools.HASHCODE + ")"
// + ")";
private static final String CREAT_TABLE_SQL = "create table " + DataTools.TABLE_NAME + "("
+ DataTools._ID + " integer primary key,"
+ DataTools.CITY + " String not null,"
+ DataTools.TEMP_CUR + " String not null,"
+ DataTools.TEMP_L + " String not null,"
+ DataTools.TEMP_H + " String not null,"
+ DataTools.STATUS + " String not null,"
+ DataTools.IMAGE + " blob not null" + ")";// blob
/**
* @param context
*/
public WeatherDataBaseHelper(Context context) {
super(context, DataTools.DB_NAME, null, DataTools.DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param name
* @param factory
* @param version
*/
public WeatherDataBaseHelper(Context context, String name, int version) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
Log.v(TAG, " WeatherDataBaseHelper-------constructor------------");
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.v(TAG, "WeatherDataBaseHelper on creat-------------------");
db.execSQL(CREAT_TABLE_SQL);
}
/**
* when version changed,updata database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v(TAG, "on upgrade");
String sql = " DROP TABLE IF EXISTS " + DataTools.TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
}
Ⅵ android中操作数据库sql语句,联合唯一索引,重复则替换,怎么修改这句sq
不会是id都相同吧
select A from 表 group by A having count(A)>1
1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
and rowid not in (select min(rowid) from people group by peopleId having count(peopleId )>1)
3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
Ⅶ 安卓execsql数据库long类型怎么表示
第一,数据库中没有long型。oracle db2等数据库中,请使用number(24)作为类型。java中long的长度好像没超过16位。如果使用很大的整数,请使用java.math包中的 bigInteger类型。 第二。你这个完全可以存储为字符串或者日期类型好么?