解决方案 »
- 绘制渐变背景的问题。
- android 音频部分 开发板
- 关于Android能否写NV的问题
- android 2.3.4 与2.3.5 、2.3.6、2.3.7后续版本都新增哪些新特性呢?谢谢!
- android中如何在已有的main.xml文件中植入新的xml布局内容
- 如何获取到多个具有相同ID的View中的某一个View?
- android开发之GridView UI篇
- 求好心人指导android帮助文档chm
- android 默认允许adb连接 不弹出提示框
- 关于BufferReader没读到数据的问题
- android 连接 cmwap 代码 网上搜了好几天了 没有一个可以用的
- 怎样实现利用WIFI在android手机间的文件传送?
public void open(Context ctx)
{
Log.i(getTag(), "Open database '" + getDatabaseName() + "'");
try
{
mDbHelper = new CreateDBHelper(ctx);
if(mDbHelper!=null)
{
mDb = mDbHelper.getWritableDatabase();
}
}
catch(SQLException e)
{
Log.e("open", e.getMessage());
}
}
AUTOINCREMENT, title TEXT, value REAL);"); 这条语句会创建一个名为 mytable 的表,表有一个列名为 _id,并且是主键,这列的值是会自动增长的整数(例如,当你插入一行时,SQLite 会给这列自动赋值),另外还有两列:title( 字符 ) 和 value( 浮点数 )。 SQLite 会自动为主键列创建索引。
package android.sqliteTest;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class sqliteTest extends Activity implements OnClickListener {
private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT;
private static final String DB_NAME = "StudDB.db";
private static final int DB_VERSION = 2;
private Cursor cur;
private Button btn, btn2, btn3, btn4, btn5;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
btn = new Button(this);
btn.setText("create Student table");
btn.setOnClickListener(this);
layout.addView(btn, new LinearLayout.LayoutParams(WC, WC));
btn2 = new Button(this);
btn2.setText("drop Student table");
btn2.setOnClickListener(this);
layout.addView(btn2, new LinearLayout.LayoutParams(WC, WC));
btn3 = new Button(this);
btn3.setText("insert a record");
btn3.setOnClickListener(this);
layout.addView(btn3, new LinearLayout.LayoutParams(WC, WC));
btn4 = new Button(this);
btn4.setText("select *");
btn4.setOnClickListener(this);
layout.addView(btn4, new LinearLayout.LayoutParams(WC, WC));
btn5 = new Button(this);
btn5.setText("Exit");
btn5.setOnClickListener(this);
layout.addView(btn5, new LinearLayout.LayoutParams(WC, WC));
setContentView(layout);
}
// ----------------------------------------------------------------------------
private DatabaseHelper mOpenHelper;
public void onClick(View v) {
if (v == btn) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "create table Student(" + "stud_no text not null, "
+ "stud_name text );";
try {
db.execSQL(sql);
setTitle("create table ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
setTitle("create table Error!");
}
}
if (v == btn2) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql = "drop table Student";
try {
db.execSQL(sql);
setTitle("drop table ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
setTitle("drop table Error!");
}
}
if (v == btn3) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql_1 = "insert into Student (stud_no, stud_name) values('S108', 'Lily Chen');";
String sql_2 = "insert into Student (stud_no, stud_name) values('S201', 'TomKao');";
String sql_3 = "insert into Student (stud_no, stud_name) values('S333', 'Peter Rabbit');";
try {
db.execSQL(sql_1);
db.execSQL(sql_2);
db.execSQL(sql_3);
setTitle("insert records ok!");
} catch (SQLException e) {
Log.e("ERROR", e.toString());
}
}
if (v == btn4) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = { "stud_no", "stud_name" };
cur = db.query("Student", col, null, null, null, null, null);
Integer n = cur.getCount();
String ss = Integer.toString(n);
setTitle(ss + " records");
cur.moveToFirst();
}
if (v == btn5)
finish();
}
}
package android.sqliteTest;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class sqliteTest2 extends Activity implements OnClickListener {
private static final String DB_NAME = "StudDB.db";
private static final int DB_VERSION = 2;
private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;
private Button btn3, btn4, btn5, btn6, btn7, btn8;
private Cursor cur;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Student(" + "stud_no"
+ " TEXT PRIMARY KEY," + "stud_name" + " TEXT" + ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
// ----------------------------------------------------------------
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
btn3 = new Button(this);
btn3.setText("insert a record");
btn3.setOnClickListener(this);
layout.addView(btn3, new LinearLayout.LayoutParams(WC, WC));
btn4 = new Button(this);
btn4.setText("select *");
btn4.setOnClickListener(this);
layout.addView(btn4, new LinearLayout.LayoutParams(WC, WC));
btn5 = new Button(this);
btn5.setText("next record");
btn5.setOnClickListener(this);
layout.addView(btn5, new LinearLayout.LayoutParams(WC, WC));
btn6 = new Button(this);
btn6.setText("update record");
btn6.setOnClickListener(this);
layout.addView(btn6, new LinearLayout.LayoutParams(WC, WC));
btn7 = new Button(this);
btn7.setText("delele record");
btn7.setOnClickListener(this);
layout.addView(btn7, new LinearLayout.LayoutParams(WC, WC));
btn8 = new Button(this);
btn8.setText("Exit");
btn8.setOnClickListener(this);
layout.addView(btn8, new LinearLayout.LayoutParams(WC, WC));
}
private DatabaseHelper mOpenHelper;
public void onClick(View v) {
if (v == btn3) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("stud_no", "S108");
cv.put("stud_name", "Lily Chen");
db.insert("Student", null, cv);
cv = new ContentValues();
cv.put("stud_no", "S201");
cv.put("stud_name", "TomKao");
db.insert("Student", null, cv);
cv = new ContentValues();
cv.put("stud_no", "S333");
cv.put("stud_name", "Peter Rabbit");
db.insert("Student", null, cv);
setTitle("insert record ok!");
}
if (v == btn4) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
String col[] = { "stud_no", "stud_name" };
cur = db.query("Student", col, null, null, null, null, null);
Integer n = cur.getCount();
String ss = Integer.toString(n);
setTitle(ss + " records");
cur.moveToFirst();
}
if (v == btn5) {
if (!cur.isAfterLast()) {
String ss = cur.getString(0) + ", " + cur.getString(1);
setTitle(ss);
cur.moveToNext();
} else
setTitle("======");
}
if (v == btn6) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("stud_no", "S288");
cv.put("stud_name", "Linda Wang");
db.update("Student", cv, "stud_no = 'S201'", null);
}
if (v == btn7) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
db.delete("Student", "stud_no = 'S108'", null);
}
if (v.equals(btn8)) {
mOpenHelper = new DatabaseHelper(v.getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
if (db != null)
db.close();
this.finish();
}
}
}
private final String CREATE_TBL = "create table favourite(_id integer primary key autoincrement, songs_id integer,favouriteGroup nvarchar(50) not null)";
private final String DB_NAME = "favourite";
//定义一个上下文变量
Context context;
public SDCardAccess(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public SDCardAccess(Context context,String dbName){
super(context,dbName,null,1);
this.context = context;
} @Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(this.CREATE_TBL);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
这里context是上下文的意思
还必须实现它的派生类!
在派生类中实现一些方法!
比如:
protected abstract String getDatabaseName();
//这个是获取数据库名的方法,通过这个方法,传入数据库名
protected abstract int getDatabaseVersion();
//这个是获取数据库的版本,通过实现这个方法,传入数据库版本! // 打开或者创建一个指定名称的数据库
public void open(Context ctx)//ctx是activity传入的上下文
{
Log.i(getTag(), "Open database '" + getDatabaseName() + "'");
try
{
mDbHelper = new CreateDBHelper(ctx);
if(mDbHelper!=null)
{
mDb = mDbHelper.getWritableDatabase();//第一次调用时,回回调onCreate()这个函数,在这个函数中创建表格!如上面的executeBatch(createDBTables(), db); 调用这个函数createDBTables()创建表格!
}
}
catch(SQLException e)
{
Log.e("open", e.getMessage());
}
}
等等的!楼主再看看吧!