您的位置:首页 > 数据库

SqliteDB 操作数据库

2015-09-12 22:11 375 查看


SqliteDB 操作数据库


创建并获取数据

第一个参数是-->sql: the SQL query. The SQL string must not be ; terminated
第二个参数是-->selectionArgs: You may include ?s in where clause in the query, which will be replaced by the values from selectionArgs. The values will be bound as Strings.


如果想得到相应列名对应的列那么需要使用:

cursor.getColumnIndex("_id")


如果想要获取数据库中相应类型的值那么需要使用如下方式

int—>cursor.getInt(相应的列的位置即上面的那个语句获取的值)
String—>cursor.getString(同上)

要注意的是如果列对应的值类型不匹配或者是空那么就会出现异常


对数据进行增删改操作

db.execSQL("insert into student values(?,?,?,?,?)",newObject[]{4,"liuneng","男",58,"666"});


2.这个一会经常使用
SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH, null,SQLiteDatabase.OPEN_READWRITE);
db.insert()
Parameters:
table :
the table to insert the row into
nullColumnHack:
optional; may be null. SQL doesn't allow inserting a completely empty row without naming at least one column name. If your provided values is empty, no column names are known and an empty row can't be inserted. If not set to null, the nullColumnHack parameter provides the name of nullable column name to explicitly insert a NULL into in the case where your values is empty.
values:
this map contains the initial column values for the row. The keys should be the column names and the values the column values
Returns:
the row ID of the newly inserted row, or -1 if an error occurred


更新
db.update
int android.database.sqlite.SQLiteDatabase.update(String table, ContentValues values, String whereClause, String[] whereArgs)
Parameters:
table the table to update in
values a map from column names to new column values. null is a valid value that will be translated to NULL.
whereClause the optional WHERE clause to apply when updating. Passing null will update all rows.
whereArgs


删除
db.delete
int android.database.sqlite.SQLiteDatabase.delete(String table, String whereClause, String[] whereArgs)
Convenience method for deleting rows in the database.
Parameters:
table the table to delete from
whereClause the optional WHERE clause to apply when deleting. Passing null will delete all rows.
whereArgs
Returns:
the number of rows affected if a whereClau
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: