SQL Android

SQLite是一款輕量級的關係型數據庫,它的運算速度很是快,佔用資源不多。數據庫

通常有如下幾個關鍵步驟:ide

1.建立數據庫this

2.建立表code

3.操做:增刪改查對象

4.關閉數據庫繼承

5.刪除表(非必選)資源

SQLiteOpenHelper是SQLiteDatabse的一個幫助類,用來管理數據的建立和版本更新。通常的用法是定義一個類繼承SQLiteOpenHelper,並實現兩個回調方法,OnCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabse, int oldVersion, int newVersion)來建立和更新數據庫。rem

 

下面新建類MyDatabaseHelper繼承自SQLiteOpenHelper,並建立了表peopleget

public class MyDatabaseHelper extends SQLiteOpenHelper {

public static final String CREATE_PEOPLE = "create table people ("+"id integer primary key autoincrement,"+"name text,"+"age int,"+"price double)";


public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}


@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//建立表
sqLiteDatabase.execSQL(CREATE_PEOPLE);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

//刪除舊錶從新建立
sqLiteDatabase.execSQL("drop table if exists people");
onCreate(sqLiteDatabase);
}
}

 

下面採用了兩種方式來展現增刪改查it

第一種:純SQL語句

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//增長version 來刪除以前的表信息
myDatabaseHelper = new MyDatabaseHelper(this,"peopleInfo.db",null,9);
//建立數據庫
myDatabaseHelper.getWritableDatabase();

//打開數據庫
SQLiteDatabase database = myDatabaseHelper.getWritableDatabase();
//新增zhangsan 18 11.11
// ContentValues values = new ContentValues();
// values.put("name","zhangsan");
// values.put("age",18);
// values.put("price",11.11);
// database.insert("people",null,values);

database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"zhangsan","18","11.11"});

//新增lisi 28 22.22
// values.clear();
// values.put("name","lisi");
// values.put("age",28);
// values.put("price",22.22);
// database.insert("people",null,values);

database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"lisi","28","22.22"});


//新增wangwu 48 44.44
// values.clear();
// values.put("name","wangwu");
// values.put("age",48);
// values.put("price",44.44);
// database.insert("people",null,values);

database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"wangwu","48","44.44"});


//刪除zhangsan信息
// database.delete("people","name = ?",new String[] {"zhangsan"});

database.execSQL("delete from people where name = ?",new String[] {"zhangsan"});

//更改lisi age=38
// values.clear();
// values.put("age",38);
// database.update("people",values,"name = ?",new String[] {"lisi"});

database.execSQL("update people set age = ? where name = ? ",new String[] {"38","lisi"});


//查詢people表的內容並打印
// Cursor cursor = database.query("people",null,null,null,null,null,null,null);
Cursor cursor = database.rawQuery("select * from people",null);
if (cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));

Log.d(TAG, "onCreate: "+name+" "+age+" "+price);

}while (cursor.moveToNext());

}
cursor.close();


}
}

 

第二種:Android對象方式


public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
private MyDatabaseHelper myDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//增長version 來刪除以前的表信息
myDatabaseHelper = new MyDatabaseHelper(this,"peopleInfo.db",null,9);
//建立數據庫
myDatabaseHelper.getWritableDatabase();

//打開數據庫
SQLiteDatabase database = myDatabaseHelper.getWritableDatabase();
//新增zhangsan 18 11.11
ContentValues values = new ContentValues();
values.put("name","zhangsan");
values.put("age",18);
values.put("price",11.11);
database.insert("people",null,values);

// database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"zhangsan","18","11.11"});

//新增lisi 28 22.22
values.clear();
values.put("name","lisi");
values.put("age",28);
values.put("price",22.22);
database.insert("people",null,values);

// database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"lisi","28","22.22"});


//新增wangwu 48 44.44
values.clear();
values.put("name","wangwu");
values.put("age",48);
values.put("price",44.44);
database.insert("people",null,values);

// database.execSQL("insert into people (name,age,price) values(?,?,?)",new String[] {"wangwu","48","44.44"});


//刪除zhangsan信息
database.delete("people","name = ?",new String[] {"zhangsan"});

// database.execSQL("delete from people where name = ?",new String[] {"zhangsan"});

//更改lisi age=38
values.clear();
values.put("age",38);
database.update("people",values,"name = ?",new String[] {"lisi"});

// database.execSQL("update people set age = ? where name = ? ",new String[] {"38","lisi"});


//查詢people表的內容並打印
Cursor cursor = database.query("people",null,null,null,null,null,null,null);
// Cursor cursor = database.rawQuery("select * from people",null);
if (cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
double price = cursor.getDouble(cursor.getColumnIndex("price"));

Log.d(TAG, "onCreate: "+name+" "+age+" "+price);

}while (cursor.moveToNext());

}
cursor.close();


}
}

 

 

最終結果

 

05-28 10:00:11.618 16802-16802/? D/MainActivity: onCreate: lisi 38 22.2205-28 10:00:11.618 16802-16802/? D/MainActivity: onCreate: wangwu 48 44.44

相關文章
相關標籤/搜索