Ⅰ 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類型。 第二。你這個完全可以存儲為字元串或者日期類型好么?