数据库的创建与操作

package com.example.mydb.test;

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

/**
 * Created by liukang on 2018/1/4 15:21.
 */

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public static final String NAME_DB = "test.db";
    public static final String NAME_TABLE = "person";
    public static final int VERSION_DB = 1;


    public MyDatabaseHelper(Context context) {
        this(context, NAME_DB, null, VERSION_DB);
    }

    public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
//        db.execSQL("create table imagetable(_id integer primary key autoincrement," +
//                "word varchar(255),detail varchar(255))");//执行创建表的sql语句
        db.execSQL("create table person (_id integer primary key autoincrement," +
                "name varchar(255)," +
                "age varchar(255)," +
                "sex varchar(255))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String sql = "DROP TABLE IF EXISTS " + NAME_TABLE;
        db.execSQL(sql);
        this.onCreate(db);
    }
}
 
 
 
 
package com.example.mydb.test;  import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase;  import java.util.List;  /**  * Created by liukang on 2018/1/4 16:13.  */  public class DbManager { static DbManager instance = null;  static Context mContext;   static SQLiteDatabase db = null;   static MyDatabaseHelper help = null;   /**  * 初始化,单例对象  */  private DbManager() { } public static DbManager getInstance(Context context) { if (instance == null) { mContext = context;  help = new MyDatabaseHelper(mContext);  db = help.getWritableDatabase();  instance = DbManagerHolder.holder;  } return instance;  } private static class DbManagerHolder { public static final DbManager holder = new DbManager();  } /**  * 查询1  */   public Cursor queryNon(String sql) { Cursor cursor = null;  try { if (db != null) { cursor = db.rawQuery(sql, null);  } } catch (Exception e) { e.printStackTrace();  return null;  } return cursor;  } /**  * 查询2  */   public Cursor query(String sql, String[] args) { Cursor cursor = null;  try { if (db != null) { cursor = db.rawQuery(sql, args);  } } catch (Exception e) { e.printStackTrace();  return null;  } return cursor;  } /**  * 执行1  */   public boolean execNonQuery(String sql) { try { if (db != null) db.execSQL(sql);  } catch (Exception e) { e.printStackTrace();  return false;  } return true;  } /**  * 执行2  */   public boolean execNonQuery(String sql, Object[] args) { if (args == null) return false;  try { if (db != null) db.execSQL(sql, args);  } catch (Exception e) { e.printStackTrace();  return false;  } return true;  } /**  * 执行列表  */   public boolean execList(List<String> list) { if (list == null && list.size() == 0) { return false;  } try { if (db != null) { db.beginTransaction();  for (int i = 0; i < list.size(); i++) { db.execSQL(list.get(i));  } db.setTransactionSuccessful();  db.endTransaction();  } } catch (Exception e) { e.printStackTrace();  return false;  } return true;  } 
//手动更新说句库 
  public void updateDB (){
       help.onUpgrade(db,MyDatabaseHelper.VERSION_DB,MyDatabaseHelper.VERSION_DB+1);
    }

}
 
package com.example.mydb;  import android.database.Cursor; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView;  import com.example.mydb.test.DbManager; import com.example.mydb.test.Person;  import java.util.ArrayList;  public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView textView1;  TextView textView2;  TextView textView3;  TextView textView4;  TextView textView5;  DbManager dbManager;   @Override  protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  textView1 = findViewById(R.id.tv_1);  textView2 = findViewById(R.id.tv_2);  textView3 = findViewById(R.id.tv_3);  textView4 = findViewById(R.id.tv_4);  textView5 = findViewById(R.id.tv_5);  textView1.setOnClickListener(this);  textView2.setOnClickListener(this);  textView3.setOnClickListener(this);  textView4.setOnClickListener(this);  textView5.setOnClickListener(this);  dbManager = DbManager.getInstance(this);  } @Override  public void onClick(View v) { switch (v.getId()) { case R.id.tv_1: String sql0 = "insert into person (name,age,sex,color) values(?,?,?,?)";  dbManager.execNonQuery(sql0, new String[]{"liukang", "18", "man","blue"});  break;  case R.id.tv_2: ArrayList<Person> list = new ArrayList<>();  String sql1 = "select * from person";  Cursor cursor = dbManager.queryNon(sql1);  if (cursor != null) { while (cursor.moveToNext()) { String name = cursor.getString(1);  String age = cursor.getString(2);  String sex = cursor.getString(3);  String color=cursor.getString(4);  Person person = new Person(name, age, sex,color);  list.add(person);  } } if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { Log.e("YCW", list.get(i).toString());  } } break;  case R.id.tv_3: String sql="delete from person where name='liukang'";  dbManager.execNonQuery(sql);  break;  case R.id.tv_4: String sql2="update person set name='liukang1' where age = 18";  dbManager.execNonQuery(sql2);  break;  case R.id.tv_5: dbManager.updateDB();  break;  default: break;  } } } 

猜你喜欢

转载自blog.csdn.net/lk2021991/article/details/78973228
今日推荐