Android SQLiteまとめ

使用SQLiteOpenHelper创建和操作数据库

データベースをオーペンする

はじめ

ソースファイル①

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySQLiteOpenHelper extends SQLiteOpenHelper {

 private static final int DB_VERSION = 1;
 private static final String DB_NAME = "recipe.db";

 public MySQLiteOpenHelper(Context context) {
  super(context, DB_NAME, null, DB_VERSION);
 }

 // DBが新規作成された時に呼び出される
 @Override
 public void onCreate(SQLiteDatabase db) {
  // デーブル作成
  db.execSQL("create table if not exists people"
    + " (_id integer primary key auto increment," + " name text,"
    + " age integer);");

  // 初期データを投入
  ContentValues values = new ContentValues();
  values.put("name", "shixinzhu");
  values.put("age", "25");
  db.insert("people", null, values);
 }

 // DBのバージョンとコンストラクタで指定したバージョンが違う時に
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

  // バージョンを比較
  if (oldVersion == 1 && newVersion == 2) {
   db.execSQL("alter table people add column email text;");
  }

 }

}
ソースファイル②

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.shinesoft.lesson.database.MySQLiteOpenHelper;

public class ShineLessonActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.shine_main);

  SQLiteOpenHelper helper = new MySQLiteOpenHelper(
    getApplicationContext());
  SQLiteDatabase db = helper.getWritableDatabase();
  db.close();

  String where = "age >= ?";
  String[] args = { "5" };

  Cursor cursor;
  cursor = db.query("peopel", null, where, args, null, null, null);
  startManagingCursor(cursor);

  String[] from = { "name", "age" };
  int[] to = { R.id.name_in_list, R.id.age_in_list };

  SimpleCursorAdapter adapter;

  adapter = new SimpleCursorAdapter(getApplicationContext(),
    R.layout.shine_listitem_row, cursor, from, to);

  ListView listView = (ListView) findViewById(R.id.listView01);
  listView.setAdapter(adapter);
 }

}

Xml layout

shine_listview01.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView01"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

shine_listitem_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/name_in_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip" />

    <TextView
        android:id="@+id/age_in_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dip" />

</LinearLayout>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值