SQLite3 触发器、视图的案例

前端之家收集整理的这篇文章主要介绍了SQLite3 触发器、视图的案例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

介绍:

安卓默认的数据是sqlite,但sqlite3.6.19之前是不支持外键的,如果有两张表需要关联,用外键是最省事的,但不支持的话怎么办呢?这里就有一个解决办法,就是用事务将两张表关联起来,并且最后生成一张视图。

现有两张表

  1. Employees
  2. Dept

视图

  1. ViewEmps:显示雇员信息和他所在的部门

创建数据库

自定义一个辅助类继承sqliteOpenHelper

1. onCreate(sqliteDatabase db):数据库被创建的时候,能够生成表,并创建视图跟触发器。
2. onUpgrade(sqliteDatabse db,int oldVersion,int newVersion):更新的时候可以删除表和创建新的表。
代码如下:
[java] view plain copy
  1. publicclassDatabaseHelperextendssqliteOpenHelper{
  2. staticfinalStringdbName="demoDB";
  3. finalStringemployeeTable="Employees";
  4. finalStringcolID="EmployeeID";
  5. finalStringcolName="EmployeeName";
  6. finalStringcolAge="Age";
  7. finalStringcolDept="Dept";
  8. finalStringdeptTable="Dept";
  9. finalStringcolDeptID="DeptID";
  10. finalStringcolDeptName="DeptName";
  11. finalStringviewEmps="ViewEmps";
构造器
copy
    publicDatabaseHelper(Contextcontext){
  1. super(context,dbName,null,33);
  2. }
创建库中的表,视图和触发器
copy
    voidonCreate(sqliteDatabasedb){
  1. //TODOAuto-generatedmethodstub
  2. db.execsql("CREATETABLE"+deptTable+"("+colDeptID+"INTEGERPRIMARYKEY,"+
  3. colDeptName+"TEXT)");
  4. db.execsql("CREATETABLE"+employeeTable+"
  5. ("+colID+"INTEGERPRIMARYKEYAUTOINCREMENT,"+
  6. colName+"TEXT,"+colAge+"Integer,"+colDept+"
  7. INTEGERNOTNULL,FOREIGNKEY("+colDept+")REFERENCES
  8. "+deptTable+"("+colDeptID+"));");
  9. //创建触发器
  10. db.execsql("CREATETRIGGERfk_empdept_deptid"+
  11. "BEFOREINSERT"+
  12. "ON"+employeeTable+
  13. "FOREACHROWBEGIN"+
  14. "SELECTCASEWHEN((SELECT"+colDeptID+"FROM"+deptTable+"
  15. WHERE"+colDeptID+"=new."+colDept+")ISNULL)"+
  16. "THENRAISE(ABORT,'ForeignKeyViolation')END;"+
  17. "END;");
  18. //创建视图
  19. db.execsql("CREATEVIEW"+viewEmps+
  20. "ASSELECT"+employeeTable+"."+colID+"AS_id,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ""+employeeTable+"."+colName+","+
  21. ""+employeeTable+"."+colAge+",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ""+deptTable+"."+colDeptName+""+
  22. "FROM"+employeeTable+"JOIN"+deptTable+
  23. "ON"+employeeTable+"."+colDept+"="+deptTable+"."+colDeptID
  24. );
  25. }

更新库中的表
copy
    voidonUpgrade(sqliteDatabasedb,153); background-color:inherit; font-weight:bold">intoldVersion,153); background-color:inherit; font-weight:bold">intnewVersion){
  1. db.execsql("DROPTABLEIFEXISTS"+employeeTable);
  2. db.execsql("DROPTABLEIFEXISTS"+deptTable);
  3. db.execsql("DROPTRIGGERIFEXISTSfk_empdept_deptid");
  4. db.execsql("DROPVIEWIFEXISTS"+viewEmps);
  5. onCreate(db);
  6. }

加入数据
[java] view plain copy
  1. sqliteDatabasedb=this.getWritableDatabase();
  2. ContentValuescv=newContentValues();
  3. cv.put(colDeptID,1);
  4. cv.put(colDeptName,"Sales");
  5. db.insert(deptTable,colDeptID,cv);
  6. 2);
  7. "IT");
  8. db.close();

更新数据
copy
    intUpdateEmp(Employeeemp)
  1. {
  2. sqliteDatabasedb= cv.put(colName,emp.getName());
  3. cv.put(colAge,emp.getAge());
  4. cv.put(colDept,emp.getDept());
  5. returndb.update(employeeTable,cv,colID+"=?",
  6. newString[]{String.valueOf(emp.getID())});
  7. }

删除数据
copy
    voidDeleteEmp(Employeeemp)
  1. db.delete(employeeTable,colID+"=?",153); background-color:inherit; font-weight:bold">newString[]{String.valueOf(emp.getID())});
  2. db.close();
  3. }

取得所有部门信息
copy @H_301_751@
    CursorgetAllDepts()
  1. this.getReadableDatabase();
  2. Cursorcur=db.rawQuery("SELECT"+colDeptID+"as_id,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "+colDeptName+"from"+deptTable,153); background-color:inherit; font-weight:bold">newString[]{});
  3. returncur;
  4. }

取得部门内雇员信息
copy
    publicCursorgetEmpByDept(StringDept)
  1. String[]columns=newString[]{"_id",colName,colAge,colDeptName};
  2. Cursorc=db.query(viewEmps,columns,colDeptName+"=?",
  3. newString[]{Dept},153); background-color:inherit; font-weight:bold">null);
  4. returnc;
  5. }

取得部门ID
copy
    intGetDeptID(StringDept)
  1. Cursorc=db.query(deptTable,newString[]{colDeptID+"as_id",colDeptName},108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> colDeptName+"=?",153); background-color:inherit; font-weight:bold">null);
  2. //Cursorc=db.rawQuery("SELECT"+colDeptID+"as_idFROM"+deptTable+"
  3. //WHERE"+colDeptName+"=?",newString[]{Dept});
  4. c.moveToFirst();
  5. returnc.getInt(c.getColumnIndex("_id"));
  6. }
原文链接:https://www.f2er.com/sqlite/200034.html

猜你在找的Sqlite相关文章