这个应该算是小型数据库了。
我在DJANGO开发中已不怎么用它啦。
APP中,试一下用法吧。
package com.tw.flag.ch15_sqlite;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final String db_name="testDB";
static final String tb_name="test";
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase(db_name, Context.MODE_PRIVATE, null);
String createTable="CREATE TABLE IF NOT EXISTS " +
tb_name +
"(name VARCHAR(32), " +
"phone VARCHAR(16), " +
"email VARCHAR(64))";
db.execSQL(createTable);
Cursor c = db.rawQuery("SELECT * FROM " + db_name, null);
if (c.getCount() == 0) {
addData("Flag Publishing Co.", "02-23423433", "service@flag.com.tw");
addData("PCDIY Magzine", "02-33267979", "service@pcdiy.com.tw");
c = db.rawQuery("SELECT * FROM " + db_name, null);
}
if (c.moveToFirst()) {
String str = "总共有 " + c.getCount() + " 项数据\n";
str += "-------------";
do {
str += "name: " + c.getString(0) + "\n";
str += "phone: " + c.getString(1) + "\n";
str += "email: " + c.getString(2) + "\n";
str += "-------------";
} while (c.moveToNext());
TextView txv = (TextView) findViewById(R.id.txv);
txv.setText(str);
}
db.close();
}
private void addData(String name, String phone, String email) {
ContentValues cv = new ContentValues(3);
cv.put("name", name);
cv.put("phone", phone);
cv.put("email", email);
db.insert(tb_name, null, cv);
}
}