当前位置:数据库 > SQLite >>

如何将SQLite3 创建的数据库与android 程序连接

答案:Sqlite3是android自带的一种轻型的数据库,使用的话也是很方便的。 下面是例子: package yeshu.sqlite3; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class sqlite3 extends Activity { private Button createdatabase; private Button updatedatabase; private Button insert; private Button update; private Button select; private Button delete; private EditText edittext01; private EditText edittext02; private String name; private int id; private String dbname; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); createdatabase = (Button) findViewById(R.id.xxx); updatedatabase = (Button)findViewById(R.id.Button02); insert = (Button)findViewById(R.id.Button03); update = (Button)findViewById(R.id.Button04); select = (Button)findViewById(R.id.Button05); delete = (Button)findViewById(R.id.Button06); edittext01 = (EditText)findViewById(R.id.EditText01); edittext02 = (EditText)findViewById(R.id.EditText02); createdatabase.setOnClickListener(new OnClickListener_create()); updatedatabase.setOnClickListener(new OnClickListener_updatedatabase()); insert.setOnClickListener(new OnClickListener_insert()); update.setOnClickListener(new OnClickListener_update()); select.setOnClickListener(new OnClickListener_select()); delete.setOnClickListener(new OnClickListener_delete()); } class OnClickListener_delete implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); db.delete("user", "id=?", new String[]{id+""}); } } class OnClickListener_insert implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub name = edittext02.getText().toString(); id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); ContentValues value = new ContentValues(); value.put("id", id); value.put("name", name); db.insert("user", null, value); System.out.println("insert"); } } class OnClickListener_update implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub name = edittext02.getText().toString(); id = Integer.parseInt(edittext01.getText().toString()); Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getWritableDatabase(); ContentValues value = new ContentValues(); value.put("name", name); db.update("user", value, "id=?", new String[]{id+""}); } } class OnClickListener_updatedatabase implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu", 2); SQLiteDatabase db = temp.getReadableDatabase(); } } class OnClickListener_select implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getReadableDatabase(); Cursor cursor = db.query("user", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null); while(cursor.moveToNext()) { System.out.println(cursor.getShort(cursor.getColumnIndex("id"))); System.out.println(cursor.getString(cursor.getColumnIndex("name"))); } } } class OnClickListener_create implements OnClickListener { public void onClick(View v) { // TODO Auto-generated method stub Data temp = new Data(sqlite3.this, "data_yeshu"); SQLiteDatabase db = temp.getReadableDatabase(); } } } ================================ package yeshu.sqlite3; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class Data extends SQLiteOpenHelper { private static final int VERSION = 1; public Data(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } public Data(Context context, String name, int version) { this(context, name, null, version); } public Data(Context context , String name) { this(context, name, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub System.out.println("create a new database"); db.execSQL("create table user(id int, name VERCHAR(20))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub System.out.println("update a database"); } }
其他:连接代码?网上搜搜。。

上一个:android listview 我想获得sqlite数据库中,表diary_list中的第3条数据。该怎么办?
下一个:当用select从sqlite3中查询出来一些数据,怎样获得这些记录的详细信息,用Xcode的,就是objective-c的。

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,