小女纸还在门槛级,求前辈们给个源码案例,带数据库开发的,而且数据库还是多表的,俺还在门槛上,没迈过去,求前辈们可怜可怜,拉一把呗。
唉,先把你问题贴上来,帮你想想,其他什么源码的去网上找找
//该类继承自SQLiteOpenHelper,创建数据库Button
public class DatabaseHelper extends SQLiteOpenHelper{ private static String DB_NAME = "Button.db"; public DatabaseHelper(Context context) { /** * CursorFactory 指定执行查询时获得一个游标实例工厂类,设置为null,则使用系统默认的工厂类 */ super(context, DB_NAME, null, 1); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub db.execSQL("DROP TABLE IF EXISTS Button"); this.onCreate(db); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL("CREATE TABLE Button(Id integer NOT NULL PRIMARY KEY UNIQUE,name text," + "parentId integer,typeId integer,CMD1 text,CMD2 text,CMD3 text,CMD1Status integer," + "CMD2Status integer,CMD3Status integer,CMD1Id integer,CMD2Id integer,CMD3Id integer," + "CMD1Param integer,CMD2Param integer,CMD3Param integer,IsDimmer integer DEFAULT 0," + "openState integer DEFAULT 0,BtnType integer)"); } }
//该类用于对数据库的增删改查操作
public class ButtonHelper { DatabaseHelper dbhelper; public ButtonHelper(Context context){ this.dbhelper = new DatabaseHelper(context); } //添加新的房间楼层 public long Save(String name,String parentId,String typeId) { ContentValues values = new ContentValues(); values.put(NAME, name); values.put(PARENTID, parentId); values.put(TYPEID, typeId); return dbhelper.getWritableDatabase().insert(TABLE_NAME, null, values); } //关闭数据库 public void close() { if(dbhelper != null){ dbhelper.close(); } } }
//此处为向数据库插入数据的代码 ,执行到这里的时候会报错空异常
Context context;
ButtonHelper btnhelper =new ButtonHelper(context);
btnhelper.Save(etName.getText().toString(), "0", "0");
@堕落为妖精:
多表查询方法:
sqlite3可以有多种多表查询方法,比如 select (select * from table2) from table1 where xxx=xxx;
这种方法比较简单并且高效:
select table1.abc from table1,table2 where table1.xxx=table2.xxx;
或这种
select table1.abc from table1 inner join table2 on table1.xxx=table2.xxx;
@az235: 没有涉及到多表查询啊 ,现在只是向一个表中插入数据,但是报错空异常,我想是Button表创建错误,