1、 创建DBhelper 来实现数库的创建

package net.android.androidDB;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;

import android.database.sqlite.SQLiteOpenHelper;

import android.database.sqlite.SQLiteDatabase.CursorFactory;

public class DBhelper extends SQLiteOpenHelper{

private static final int VERSION = 1;

public DBhelper(Context context, String name, CursorFactory factory,

int version) {

super(context, name, factory, version);

}

public DBhelper(Context context, String name) {

this(context, name, VERSION);

}

public DBhelper(Context context, String name, int version) {

this(context, name, null, version);

}

@Override

public void onCreate(SQLiteDatabase arg0) {

System.out.println("create a DB");

// arg0.execSQL("create table user(id int, name varchar(20))");

}

@Override

public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {

System.out.println("update a DB");

}

}

2、 创建运行类

package net.android.android_db;

import java.util.Date;

import java.util.Random;

import android.app.Activity;

import android.content.ContentValues;

import android.content.Context;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

import com.ko8e.DBhelper;

public class android_db extends Activity {

/** Called when the activity is first created. */

private Button button1 = null;

private Button button2 = null;

private Button button3 = null;

private Button button4 = null;

private Button button5 = null;

private TextView show=null;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

show=(TextView)findViewById(R.id.show);

button1 = (Button) findViewById(R.id.button1);

button2 = (Button) findViewById(R.id.button2);

button3 = (Button) findViewById(R.id.button3);

button4 = (Button) findViewById(R.id.button4);

button5 = (Button) findViewById(R.id.button5);

button1.setOnClickListener(new CreateListener());

button2.setOnClickListener(new UpdateRecordListener());

button3.setOnClickListener(new InsertListener());

button4.setOnClickListener(new QueryListener());

button5.setOnClickListener(new UpdateListener());

}

private class CreateListener implements OnClickListener {

@Override

public void onClick(View v) {

DBhelper db = new DBhelper(android_db.this, "ko8e_db");

SQLiteDatabase sqld = db.getReadableDatabase();

}

}

private class UpdateRecordListener implements OnClickListener {

@Override

public void onClick(View v) {

DBhelper db = new DBhelper(android_db.this, "ko8e_db");

SQLiteDatabase sqld = db.getReadableDatabase();

ContentValues values = new ContentValues();

values.put("name", "kobe bryant");

sqld.update("user", values, "id=?", new String[]{"1"});

}

}

private class InsertListener implements OnClickListener {

@Override

public void onClick(View v) {

ContentValues values = new ContentValues();

values.put("id", 1);

Random ran=new Random();

values.put("name", new Date().toString()+" "+String.valueOf(ran.nextInt(10000))+" ko8e");

DBhelper db = new DBhelper(android_db.this, "ko8e_db", Context.MODE_WORLD_WRITEABLE);

SQLiteDatabase sqld = db.getReadableDatabase();

sqld.insert("user", null, values);

}

}

private class QueryListener implements OnClickListener {

@Override

public void onClick(View v) {

DBhelper db = new DBhelper(android_db.this, "ko8e_db");

SQLiteDatabase sqld = db.getReadableDatabase();

Cursor cursor = sqld.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);

String resu="";

while(cursor.moveToNext()) {

String name = cursor.getString(cursor.getColumnIndex("name"));

//System.out.println("quer " + name);

Log.e("****query", name);

resu+=" name:"+cursor.getString(cursor.getColumnIndex("name"))+" aa /r/n";

}

Log.e("**result*", resu);

// show.setText("dddd");

}

}

private class UpdateListener implements OnClickListener {

@Override

public void onClick(View v) {

DBhelper db = new DBhelper(android_db.this, "ko8e_db", 2);

SQLiteDatabase sqld = db.getReadableDatabase();

}

}

}

3、 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">

<Button android:id="@+id/btn_create" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="创建表(含库)" />

<Button android:id="@+id/btn_insert" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="添加记录" />

<Button android:id="@+id/btn_query" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="查询记录" />

<Button android:id="@+id/btn_update" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="更新记录" />

<Button android:id="@+id/btn_delete" android:layout_width="fill_parent"

android:layout_height="wrap_content" android:text="删除记录" />

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal" android:layout_width="fill_parent"

android:layout_height="wrap_content">

<TextView android:text="ID" android:layout_width="wrap_content"

android:layout_height="wrap_content" />

<EditText android:id="@+id/edID" android:layout_width="100px"

android:layout_height="wrap_content" />

</LinearLayout>

<TextView android:id="@+id/show" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:text="show"/>

</LinearLayout>

更多相关文章

  1. Mac上Android(安卓)studio环境搭建
  2. Android得到SD卡文件夹大小以及删除文件夹操作
  3. Android(安卓)Studio第三十四期 - git企业级应用命令
  4. Android中遇到问题:file.delete()不能删除文件
  5. Android获取SDcard目录及创建文件夹;
  6. [APP] Android(安卓)开发笔记 001-环境搭建与命令行创建项目
  7. android-数据库操作实例,留着以后用
  8. Android(安卓)8.0和8.1通知栏
  9. Android关于8.0以上Service服务相关问题记录

随机推荐

  1. android中的定时任务AlarmManager
  2. Android(安卓)Splash Activity Demo
  3. android listview 调用sqlsite数据库显示
  4. Android(安卓)drivers to be included in
  5. Android(安卓)Out of Memory Error: Caus
  6. Android(安卓)播放音乐文件与视频文
  7. android的listactivity实例
  8. Android开发环境搭建
  9. Android解决ImageView setRotation....等
  10. Android(安卓)issues