新手SQLite数据库操作(转)

来源:互联网 发布:什么是计算机域名 编辑:程序博客网 时间:2024/06/01 01:33

fu.java 文件 


package demo.fu;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class fu extends Activity {
/* 全局变量 */
private static String DATABASE_NAME = "ENEST";
private static int DATABASE_VERSION = 2;
public static String TABLE_NAME_ONE = "ONE";
public static String TITLE_ONE = "Title_one";
public static String BODY_ONE = "Body_one";

public static DatabaseHelper mOpenHelper;
private static int Num_0 = 0;
private static int Num_1 = 1;
private static int Num_2 = 2;
public static ArrayList<Map<String, Object>> arraylistmap;
public static Cursor myCursor_one;

public static String mytitle;
public static String mybody;
public static HashMap<String, Object> mymap;
/* 数据库HELPER类 */
static class DatabaseHelper extends SQLiteOpenHelper {
  public DatabaseHelper(Context context) {
  
   super(context, DATABASE_NAME, null, DATABASE_VERSION);
   // TODO Auto-generated constructor stub
  }
  @Override
  public void onCreate(SQLiteDatabase db) {
      //新建一个表,以及表内的字段。
   //TODO Auto-generated method stub
   //String sql = "CREATE TABLE " + TABLE_NAME_ONE + "(" + TITLE_ONE
   //+ " text not null, " + BODY_ONE + " text not null " + ");";
   //db.execSQL(sql);
  }
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
}
/* 创建TABLE ONE */
private void create_one() {
  SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  String sql = "CREATE TABLE " + TABLE_NAME_ONE + "(" + TITLE_ONE
    + " text not null, " + BODY_ONE + " text not null " + ");";
  try {
   db.execSQL("DROP TABLE IF EXISTS diary");
   db.execSQL(sql);
   setTitle("Table one create succeed");
  } catch (SQLException e) {
   setTitle("Table one create fault");
  }
 
  Toast.makeText(fu.this, "新建了一个表  one", Toast.LENGTH_LONG).show();
}

/* 往TABLE ONE 中插入预定数据 */
private void insertTable_one() {
  SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  String sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
    + BODY_ONE + ") values('" + Num_0 + "','[one]this is 0');";
  db.execSQL(sql);
  Log.e("table one ", "num 0 has insert");
  sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
    + BODY_ONE + ") values('" + Num_1 + "','[one]this is 1');";
  db.execSQL(sql);
  Log.e("table one ", "num 1 has insert");
  sql = "insert into " + TABLE_NAME_ONE + " (" + TITLE_ONE + ", "
    + BODY_ONE + ") values('" + Num_2 + "','[one]this is 2');";
  db.execSQL(sql);
  Log.e("table one ", "num 2 has insert");
  setTitle("Table one insert succeed");
 
  Toast.makeText(fu.this, "插入了数据撒~~~", Toast.LENGTH_LONG).show();
}
/* 往TABLE TWO 中插入预定数据 */

/* 删除TABLE ONE */
private void dropTable_one() {
  SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  String sql = "drop table " + TABLE_NAME_ONE;
  try {
   db.execSQL(sql);
   setTitle("DB one delete succeed:" + sql);
  } catch (SQLException e) {
   setTitle("DB one delete falut");
  }
  Toast.makeText(fu.this, "删除了表。。。", Toast.LENGTH_LONG).show();
}

/* 显示出TABLE ONE 跳转到SQLVIEWLIST.JAVA */
private void show_one() {
  SQLiteDatabase db = mOpenHelper.getWritableDatabase();
  myCursor_one = db.rawQuery("SELECT * FROM ONE", null);
  int first = myCursor_one.getColumnIndex(TITLE_ONE);
  int second = myCursor_one.getColumnIndex(BODY_ONE);
  arraylistmap.clear();
  if (myCursor_one.moveToFirst()) {
   do {
    mytitle = myCursor_one.getString(first);
    mybody = myCursor_one.getString(second);
    mymap = new HashMap<String, Object>();
    mymap.put(TITLE_ONE, mytitle);
    mymap.put(BODY_ONE, mybody);
    arraylistmap.add(mymap);
   } while (myCursor_one.moveToNext());
  }
  myCursor_one.close(); 
 
     ListView listView = new ListView(this);
  /*
   * ADAPTER将ARRAYLISTMAP中的数据以COPY_MAIN的形式输出,TITLE_ONE数据对应LIST_TITLE,
   * BODY_ONE数据对应到LIST_BODY
   */
  SimpleAdapter adapter = new SimpleAdapter(this,
    fu.arraylistmap, R.layout.copy_main, new String[] {
    fu.TITLE_ONE, fu.BODY_ONE }, new int[] {
      R.id.list_title, R.id.list_body });
  listView.setAdapter(adapter);
  /* 读取游标MYCURSOR_ONE读到的数据条目数,设置为TITLE */
  int title = fu.myCursor_one.getCount();
  setTitle("Count of Table One: " + title);
  setContentView(listView);
 
 
  }

 


/*全局变量的初始化 按钮的响应 以及设置显示*/
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mOpenHelper = new DatabaseHelper(this);
  arraylistmap = new ArrayList<Map<String, Object>>();
 
  Button button1 = (Button) findViewById(R.id.button01);
  button1.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    create_one();
   }
  });
  Button button2 = (Button) findViewById(R.id.button02);
  button2.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    dropTable_one();
   }
  });
  Button button3 = (Button) findViewById(R.id.button03);
  button3.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    insertTable_one();
   }
  });
  Button button4 = (Button) findViewById(R.id.button04);
  button4.setOnClickListener(new Button.OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    show_one();
   
   }
  });

}
}

main.xml  文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout android:layout_width="fill_parent"
    android:orientation="horizontal"
    android:layout_height="80dip">
    <Button android:id="@+id/button01" android:text="create one" android:layout_height="80dip" android:layout_width="80dip"></Button>
    <Button android:id="@+id/button02" android:text="drop the one" android:layout_height="80dip" android:layout_width="80dip"></Button>
    <Button android:id="@+id/button03" android:text="insert one" android:layout_height="80dip" android:layout_width="80dip"></Button>
    <Button android:id="@+id/button04" android:text="show one" android:layout_height="80dip" android:layout_width="80dip"></Button>
    </LinearLayout>
   
</LinearLayout>

copy_main.xml文件


<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
     android:id="@+id/list_title"
     android:layout_height="wrap_content" android:layout_width="200dip"
     android:textSize="30sp"
     android:textStyle="bold"/>
    <TextView
     android:id="@+id/list_body"
     android:layout_height="wrap_content" android:layout_width="200dip"
     />
</LinearLayout>

 

图一         图二       图三         图四
3.jpg

1.jpg

2.jpg

4.jpg

2010-11-16 15:52 上传
下载附件 (11.14 KB)
 

原创粉丝点击