SQLlite
增删改查
增(insert into (表名)(参数列表) values(?,?,…,?)
public void click(View view) {
// ContentValues contentValues = new ContentValues();
// contentValues.put("name","大美");
// db.insert("users",null,contentValues);
String sql = "insert into users(name) values(?)";
db.execSQL(sql,new Object[]{"卤蛋"});
}
删(delete from (表名) where 条件语句)
public void click(View view) {
// ContentValues contentValues = new ContentValues();
// contentValues.put("name","大美");
// db.insert("users",null,contentValues);
String sql = "insert into users(name) values(?)";
db.execSQL(sql,new Object[]{"卤蛋"});
}
改(update 表名 set 字段 = 值 where 条件语句)
public void update(View view) {
// String sql = "update users set name = ? where id = ?";
// db.execSQL(sql,new Object[]{"高露洁",2});
ContentValues contentValues = new ContentValues();
contentValues.put("name","黑人");
db.update("users",contentValues,"id=?",new String[]{"1"});
}
查(select * from 表名 where 条件语句)
public void slect(View view) {
// Cursor cursor = db.query("users", null, null, null, null, null, null);
//
// if (cursor != null) {
// while (cursor.moveToNext()) {
// String name = cursor.getString(cursor.getColumnIndex("name"));
// int id = cursor.getInt(cursor.getColumnIndex("id"));
// Toast.makeText(this, name + "***" + id, Toast.LENGTH_SHORT).show();
// Log.i(TAG, "slect: "+ id + "---" + name);
// }
// }
// cursor.close();
String sql = "select * from users";
Cursor cursor = db.rawQuery(sql, new String[]{});
if (cursor != null) {
while (cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));
int id = cursor.getInt(cursor.getColumnIndex("id"));
Toast.makeText(this, name + "***" + id, Toast.LENGTH_SHORT).show();
Log.i(TAG, "slect: "+ id + "---" + name);
}
}
cursor.close();
// String sql = "select * from users";
// db.execSQL(sql);
}
SQLiteOpenHelper
package com.example.day1014.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlHelp extends SQLiteOpenHelper {
public MySqlHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
/**
* Called when the database is created for the first time. This is where the
* creation of tables and the initial population of the tables should happen.
*
* @param db The database.
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table users(id integer primary key autoincrement,name varchar(20))");
db.beginTransaction();
for (int i = 0; i < 1000; i++) {
db.execSQL("insert into users values(null,'大板牙')");
}
db.setTransactionSuccessful();
db.endTransaction();
}
/**
* Called when the database needs to be upgraded. The implementation
* should use this method to drop tables, add tables, or do anything else it
* needs to upgrade to the new schema version.
*
* <p>
* The SQLite ALTER TABLE documentation can be found
* <a href="http://sqlite.org/lang_altertable.html">here</a>. If you add new columns
* you can use ALTER TABLE to insert them into a live table. If you rename or remove columns
* you can use ALTER TABLE to rename the old table, then create the new table and then
* populate the new table with the contents of the old table.
* </p><p>
* This method executes within a transaction. If an exception is thrown, all changes
* will automatically be rolled back.
* </p>
*
* @param db The database.
* @param oldVersion The old database version.
* @param newVersion The new database version.
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}