package com.example.databasetest; import android.os.Bundle; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private MyDatabaseHelper dbHelper; Button createDatabase,addData, updateData, deleteButton, queryButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper = new MyDatabaseHelper(this,"BookStore.db",null,2); createDatabase = (Button) findViewById(R.id.create_database); addData= (Button)findViewById(R.id.add_data); updateData = (Button)findViewById(R.id.update_data); deleteButton=(Button)findViewById(R.id.delete_data); queryButton = (Button)findViewById(R.id.query_data); createDatabase.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { dbHelper.getWritableDatabase(); //创建数据库 } }); addData.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); //获取数据库对象 ContentValues cv = new ContentValues(); cv.put("name", "apple"); cv.put("price", 10.99); cv.put("pages", 236); cv.put("author","A"); db.insert("Book", null, cv); cv.clear(); cv.put("name", "The Lost Symbol"); cv.put("author", "Dan Brown"); cv.put("pages", 510); cv.put("price", 19.95); db.insert("Book", null, cv);//插入第二条数据 } }); queryButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SQLiteDatabase db = dbHelper.getWritableDatabase(); //查询Book表中所有的数据,cursor为当前执行对象的游标 Cursor cursor = db.query("Book", null, null, null, null, null, null); //如果数据库中有数据就往下执行 if (cursor.moveToFirst()) { do { //遍历Cursor对象,去除数据并打印 String name = cursor.getString(cursor .getColumnIndex("name")); String author = cursor.getString(cursor .getColumnIndex("author")); int pages = cursor.getInt(cursor .getColumnIndex("pages")); double price = cursor.getDouble(cursor .getColumnIndex("price")); Log.d("MainActivity", "book name is " + name); Log.d("MainActivity", "book author is " + author); Log.d("MainActivity", "book pages is " + pages); Log.d("MainActivity", "book price is " + price); } while (cursor.moveToNext()); } cursor.close();//关闭cursor } }); } }
</pre><pre code_snippet_id="1585783" snippet_file_name="blog_20160223_3_4885881" name="code" class="java"><pre name="code" class="java">package com.example.databasetest; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MyDatabaseHelper extends SQLiteOpenHelper { //为要创建的数据库设置列名 public static final String CREATE_BOOK = "create table Book ("+" id integer primary key autoincrement," +" author text,"+" price real,"+" pages integer,"+" name text)"; public static final String CREATE_CATERGORY = "create table Catergory ("+" id integer primary key autoincrement," +"category_name text,"+" category_code integer)"; public MyDatabaseHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_BOOK); db.execSQL(CREATE_CATERGORY); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { //如果已经存在这两张表,则删除它们,并且用Creat重新创建 db.execSQL("drop table if exists Book"); db.execSQL("drop table if exists Catergory"); onCreate(db); } }