标签:var 回车 保存 creat 成功 public 增删改查 接口 demo
/** * * @ context 上下文 * @ name 数据库名称 * @ factory 游标工厂 * @ version 版本号 */ public DataBaseHelper( Context context) { super(context, Constants.DATABASE_NAME, null, Constants.DATABASE_VERSION); }
@Override public void onCreate(SQLiteDatabase db) { //创建数据库,第一次创建时才会调用 //创建字段 //Log.d(TAG,"创建数据库...."); //sql: create table table_name(id integer,name varchar,age integer,salary integer); String sql="create table "+Constants.TABLE_NAME+"(id integer,name varchar,age integer,salary integer)"; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //更新数据库 只有newVersion高于oldVersion时才会调用 //newVersion=oldVersion时不会调用 //newVersion<oldVersion时会报错 // Log.d(TAG,"升级数据库...."); // String sql; switch (oldVersion){ case 1: sql ="alter table "+Constants.TABLE_NAME+" add phone integer"; db.execSQL(sql); break; case 2: break; } }
DataBaseHelper helper = new DataBaseHelper(this); helper.getWritableDatabase();
创建成功的表结构:(与onCreative里面的表结构一样)
创建成功的文件结构:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //更新数据库 只有newVersion高于oldVersion时才会调用 //newVersion=oldVersion时不会调用 //newVersion<oldVersion时会报错 // Log.d(TAG,"升级数据库...."); // String sql; switch (oldVersion){ case 1: sql ="alter table "+Constants.TABLE_NAME+" add phone integer"; db.execSQL(sql); break; case 2: break; } }
package com.example.databasedemo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建数据库 DatabaseHelper helper =new DatabaseHelper(this); helper.getWritableDatabase(); Dao dao =new Dao(getApplicationContext()); // dao.insert(); // dao.update(); // dao.query(); dao.delete(); } }
package com.example.databasedemo; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; /** * 这个类用于对数据库的增删改查 */ public class Dao { private final DatabaseHelper mHelper; private static final String TAG="Dao"; public Dao(Context context){ //创建数据库 //Ctrl+Alt+F,之后加回车,创建成员变量 mHelper = new DatabaseHelper(context); } public void insert(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="insert into "+Constants.TABLE_NAME+"(_id,name,age,salary,phone) values(?,?,?,?,?)"; Object []obj={1,"haohao",20,10000,1234567890}; db.execSQL(sql,obj); db.close(); } public void delete(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="delete from "+Constants.TABLE_NAME+" where name = ?"; Object []obj={"haohao"}; db.execSQL(sql,obj); db.close(); } public void update(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="update "+Constants.TABLE_NAME+" set salary= ? where name = ?"; Object []obj={20000,"haohao"}; db.execSQL(sql,obj); db.close(); } public void query(){ SQLiteDatabase db = mHelper.getWritableDatabase(); String sql="select * from "+Constants.TABLE_NAME+" where name = ?"; String []obj={"haohao"}; Cursor cursor = db.rawQuery(sql, obj); while (cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); String age = cursor.getString(cursor.getColumnIndex("age")); Log.d(TAG,"名字:"+name+"年龄:"+age); } cursor.close(); db.close(); } }
标签:var 回车 保存 creat 成功 public 增删改查 接口 demo
原文地址:https://www.cnblogs.com/suanai/p/12271577.html