一个android操作sql例子
package jp.javadrive.android;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.io.FileNotFoundException;
import android.database.SQLException;
import android.content.ContentValues;
import android.util.Log;
import android.database.sqlite.SQLiteCursor;
public class Test01_01 extends Activity implements OnClickListener{
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private Button buttonInsert;
private Button buttonDisp;
private EditText editName;
private EditText editPrice;
private SQLiteDatabase db;
private int DB_VERSION = 1;
private int DB_MODE = Context.MODE_PRIVATE;
private String DB_NAME = "db_select_01";
private String TABLE_NAME = "product";
private TextView textResult;
@Override protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
db = null;
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
LinearLayout dataLayout = new LinearLayout(this);
dataLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(dataLayout, createParam(WC, WC));
TextView textName = new TextView(this);
textName.setText("name");
dataLayout.addView(textName, createParam(WC, WC));
editName = new EditText(this);
editName.setWidth(80);
dataLayout.addView(editName, createParam(WC, WC));
TextView textPrice = new TextView(this);
textPrice.setText("price");
dataLayout.addView(textPrice, createParam(WC, WC));
editPrice = new EditText(this);
editPrice.setWidth(80);
dataLayout.addView(editPrice, createParam(WC, WC));
LinearLayout buttonLayout = new LinearLayout(this);
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(buttonLayout, createParam(WC, WC));
buttonInsert = new Button(this);
buttonInsert.setText("INSERT");
buttonInsert.setOnClickListener(this);
buttonLayout.addView(buttonInsert, createParam(WC, WC));
LinearLayout listLayout = new LinearLayout(this);
listLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.addView(listLayout, createParam(WC, WC));
buttonDisp = new Button(this);
buttonDisp.setText("Disp Data");
buttonDisp.setOnClickListener(this);
listLayout.addView(buttonDisp, createParam(WC, WC));
textResult = new TextView(this);
textResult.setText("");
listLayout.addView(textResult, createParam(WC, WC));
openDatabase();
}
private LinearLayout.LayoutParams createParam(int w, int h){
return new LinearLayout.LayoutParams(w, h);
}
private void openDatabase(){
try {
db = openDatabase(DB_NAME, null);
} catch (FileNotFoundException e) {
try {
db = createDatabase(DB_NAME, DB_VERSION, DB_MODE, null);
createTable();
} catch (FileNotFoundException e2) {
db = null;
Log.e("ERROR", e2.toString());
}
}
}
private void createTable(){
String sql = "create table " + TABLE_NAME + " ("
+ "id integer primary key autoincrement, "
+ "name text not null, "
+ "price integer);";
try {
db.execSQL(sql);
} catch (SQLException e) {
Log.e("ERROR", e.toString());
}
}
public void onClick(View v) {
if (v == buttonInsert){
String name = editName.getText().toString();
String price = editPrice.getText().toString();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("price", price);
db.insert(TABLE_NAME, null, cv);
editName.setText("");
editPrice.setText("");
}else if (v == buttonDisp){
String sql = "select * from " + TABLE_NAME + ";";
try {
SQLiteCursor c = (SQLiteCursor)db.query(sql, null);
int rowcount = c.count();
StringBuffer sb = new StringBuffer();
c.first();
for (int i = 0; i < rowcount ; i++) {
int id = c.getInt(0);
String name = c.getString(1);
int price = c.getInt(2);
sb.append("[" + id + "," + name + "," + price + "]\n");
c.next();
}
textResult.setText(new String(sb));
} catch (SQLException e) {
Log.e("ERROR", e.toString());
}
}
}
}
本文提供了一个 Android 应用中操作 SQLite 数据库的示例代码。该示例展示了如何创建数据库及表,插入数据,并从数据库中查询数据进行展示。
806

被折叠的 条评论
为什么被折叠?



