DBMNG数据库管理与应用

独立思考能力,对于从事科学研究或其他任何工作,都是十分必要的。
当前位置:首页 > 移动应用 > Android

Android数据库操作SQLLite相关代码

 在Android 手机操作系统进行实际开发中,进场会应用到数据库。而且在这一平台中对数据库的应用方法比较简单灵活。我们在这里就为大家详细介绍了相关方法,希望可以给大家带来一些帮助。

  昨天进行了GUI界面设计,感受了一下android初次设计的愉悦,今天接着学习其SQLite数据库试用,将昨天的例子中数据存到数库中,并读取查看一下。 具体看代码(原写的有点问题,再改写如下):

  1) Android数据库之库操作类:

  package com.topsun;

  import android.content.Context;

  import android.database.Cursor;

  import android.database.sqlite.SQLiteDatabase;

  import android.util.Log;

  public class DBHelper {

  private static final String TAG = "UserDB_DBHelper.java";

  private static final String DataBaseName = "UserDB";

  SQLiteDatabase db;

  Context context;

  public DBHelper(Context context) {

  this.open(context);

  }

  private void createTabel() {

  // TODO Auto-generated method stub

  String sql = "";

  try {

  sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER

  PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)";

  this.db.execSQL(sql);

  Log.v(TAG, "Create Table TestUser ok");

  } catch (Exception e) {

  Log.v(TAG, "Create Table TestUser fail");

  } finally {

  //this.db.close();

  Log.v(TAG, "Create Table TestUser ");

  }

  }

  public boolean save(String name, String sex, Integer ages) {

  String sql = "insert into TestUser values

  (null,'" + name + "','" + sex

  + "'," + ages + ")";

  try {

  this.db.execSQL(sql);

  Log.v(TAG, "insert Table TestUser 1 record ok");

  return true;

第1页 第2页 第3页 第4页 


  在Android 手机操作系统进行实际开发中,进场会应用到数据库。而且在这一平台中对数据库的应用方法比较简单灵活。我们在这里就为大家详细介绍了相关方法,希望可以给大家带来一些帮助。

  昨天进行了GUI界面设计,感受了一下android初次设计的愉悦,今天接着学习其SQLite数据库试用,将昨天的例子中数据存到数库中,并读取查看一下。 具体看代码(原写的有点问题,再改写如下):

  1) Android数据库之库操作类:

  package com.topsun;

  import android.content.Context;

  import android.database.Cursor;

  import android.database.sqlite.SQLiteDatabase;

  import android.util.Log;

  public class DBHelper {

  private static final String TAG = "UserDB_DBHelper.java";

  private static final String DataBaseName = "UserDB";

  SQLiteDatabase db;

  Context context;

  public DBHelper(Context context) {

  this.open(context);

  }

  private void createTabel() {

  // TODO Auto-generated method stub

  String sql = "";

  try {

  sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER

  PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)";

  this.db.execSQL(sql);

  Log.v(TAG, "Create Table TestUser ok");

  } catch (Exception e) {

  Log.v(TAG, "Create Table TestUser fail");

  } finally {

  //this.db.close();

  Log.v(TAG, "Create Table TestUser ");

  }

  }

  public boolean save(String name, String sex, Integer ages) {

  String sql = "insert into TestUser values

  (null,'" + name + "','" + sex

  + "'," + ages + ")";

  try {

  this.db.execSQL(sql);

  Log.v(TAG, "insert Table TestUser 1 record ok");

  return true;

第1页 第2页 第3页 第4页 


  接上页

  } catch (Exception e) {

  Log.v(TAG, "insert Table TestUser 1 record fail");

  return false;

  } finally {

  //this.db.close();

  Log.v(TAG, "insert Table TestUser ");

  }

  }

  public Cursor loadAll() {

  Cursor cur = db.query("TestUser", new String[]

  { "ID", "NAME","SEX","AGES"}, null,

  null, null, null, null);

  return cur;

  }

  public void open(Context context){

  if (null == db || !this.db.isOpen()){

  this.context = context;

  this.db = context.openOrCreateDatabase(this.DataBaseName,

  context.MODE_PRIVATE, null);

  createTabel();

  Log.v(this.TAG, "create or Open DataBase。。。");

  }

  }

  public void close() {

  db.close();

  }

  }

  package com.topsun;

  import android.content.Context;

  import android.database.Cursor;

  import android.database.sqlite.SQLiteDatabase;

  import android.util.Log;

  public class DBHelper {

  private static final String TAG = "UserDB_DBHelper.java";

  private static final String DataBaseName = "UserDB";

  SQLiteDatabase db;

  Context context;

  public DBHelper(Context context) {

  this.open(context);

  }

  private void createTabel() {

  // TODO Auto-generated method stub

  String sql = "";

  try {

  sql = "CREATE TABLE IF NOT EXISTS TestUser

  (ID INTEGER PRIMARY KEY autoincrement,

  NAME TEXT, SEX TEXT, AGES INTEGER)";

  this.db.execSQL(sql);

  Log.v(TAG, "Create Table TestUser ok");

  } catch (Exception e) {

  Log.v(TAG, "Create Table TestUser fail");

  } finally {

  //this.db.close();

  Log.v(TAG, "Create Table TestUser ");

第1页 第2页 第3页 第4页 


  接上页

  }

  }

  public boolean save(String name, String sex, Integer ages) {

  String sql = "insert into TestUser values

  (null,'" + name + "','" + sex

  + "'," + ages + ")";

  try {

  this.db.execSQL(sql);

  Log.v(TAG, "insert Table TestUser 1 record ok");

  return true;

  } catch (Exception e) {

  Log.v(TAG, "insert Table TestUser 1 record fail");

  return false;

  } finally {

  //this.db.close();

  Log.v(TAG, "insert Table TestUser ");

  }

  }

  public Cursor loadAll() {

  Cursor cur = db.query("TestUser", new String[]

  { "ID", "NAME","SEX","AGES"}, null,

  null, null, null, null);

  return cur;

  }

  public void open(Context context){

  if (null == db || !this.db.isOpen()){

  this.context = context;

  this.db = context.openOrCreateDatabase(this.DataBaseName,

  context.MODE_PRIVATE, null);

  createTabel();

  Log.v(this.TAG, "create or Open DataBase。。。");

  }

  }

  public void close() {

  db.close();

  }

  }

  2) Android数据库交互代码

  package com.topsun;

  import android.app.Activity;

  import android.database.Cursor;

  import android.os.Bundle;

  import android.util.Log;

  import android.view.View;

  import android.view.View.OnClickListener;

  import android.widget.Button;

  import android.widget.EditText;

  public class guiwindows extends Activity

  implements OnClickListener {

  EditText TEditname;

  EditText TEditsex;

  EditText TEditages;

  EditText TEditmerge;

  Button TSavebutton;

  Button TViewbutton;

  DBHelper db;

第1页 第2页 第3页 第4页 


  接上页

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

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  this.TEditname = (EditText) this.findViewById

  (R.id.widgeteditname);

  this.TEditsex = (EditText) this.findViewById

  (R.id.widgeteditsex);

  this.TEditages = (EditText) this.findViewById

  (R.id.widgeteditages);

  TEditmerge = (EditText) this.findViewById

  (R.id.widgeteditmerge);

  this.TSavebutton = (Button) this.findViewById

  (R.id.widgetSavebutton);

  TViewbutton = (Button) this.findViewById

  (R.id.widgetViewbutton);

  this.db = new DBHelper(this);

  this.TSavebutton.setOnClickListener(this);

  this.TViewbutton.setOnClickListener(this);

  }

  @Override

  public void onClick(View v) {

  // TODO Auto-generated method stub

  // this.TEditages.setText(this.TEditname.getText().

  toString()+this.TEditsex.getText().toString());

  if (v.getId() == R.id.widgetSavebutton) {

  try {

  this.db.open(this);

  this.db.save(this.TEditname.getText().toString(), this.TEditsex

  .getText().toString(), Integer.valueOf(this.TEditages

  .getText().toString()));

  } catch (Exception e) {

  Log.v("save data", "save data fail");

  } finally {

  this.db.close();

  }

  } else if (v.getId() == R.id.widgetViewbutton && null != db) {

  this.db.open(this);

  // 浏览所有数据

  Cursor cur = db.loadAll();

  StringBuffer sf = new StringBuffer();

  cur.moveToFirst();

  while (!cur.isAfterLast()) {

  sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1))

  .append(" : ").append(cur.getString(2)).append(" : ")

  .append(cur.getInt(3)).append(" ");

  cur.moveToNext();

  }

  db.close();

  this.TEditmerge.setText(sf.toString());

  }

  }

  }



from:http://www.elexcon.com/news/55397.html
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号