我已经决定讨厌sql,并且为此目的,我试图创建一个可以反复使用的辅助类,而不必再次对其进行混乱,但是它无法正常工作!
这是我现在所拥有的:
DBAssistant.java
public class DBAssistant { sqliteDatabase db = null; public DBAssistant (){ } /** Opens database **/ public voID openDB(Context context, String name){ db = new DBOpenHelper(context, name, DB_VERSION); } /** Creates the table "tablename" with the columns contained in "cols" **/ public voID createtable(String tablename, String[] cols){ String table = "CREATE table IF NOT EXISTS " + tablename + " ("; int numOfColums = cols.length; columnnames = cols; for (int i = 0; i < (numOfColums - 1); i++){ table += cols[i] + " VARCHAR, "; } table += cols[numOfColums - 1] + " VARCHAR);"; try{ db.execsql("" + table); System.out.println("Execsql:" + table); } catch(Exception e){ System.out.println(e); } System.out.println("Column names: "); for (String c : getColnames(tablename)){ System.out.print(c + " "); } } /** Inserts "data" into new row **/ public voID insertRow(String tablename, String[] data){ int cols = getCols(tablename); System.out.println("if ((data.length) = " + (data.length) + ") ==" + " (getCols(tablename) = " + getCols(tablename) + ")?"); if (data.length == cols){ System.out.println("InsIDe if loop"); String cmd = "INSERT INTO " + tablename + " ("; for (int i = 0; i < cols - 1; i++){ cmd += getColnames(tablename)[i] + ", "; } cmd += getColnames(tablename)[cols - 1] + ") VALUES ('"; for (int k = 0; k < data.length - 1; k++){ cmd += data[k] + "', '"; } cmd += "');"; System.out.println(cmd); db.execsql(cmd); } else{ System.out.println("InsIDe else loop"); String dat = ""; String sCols = ""; for (String d : data) dat += "'" + d + "'"; for (String c : getColnames(tablename)) sCols += "'" + c + "'"; System.out.println("-------------------"); System.out.println("[insertRow] ERROR: Number of elements in data[" + dat + "]"); System.out.println("doesnt match the number of columns [" + cols + "] in " + tablename); System.out.println("-------------------"); } } /** Return String[] containing the column names in tablename **/ public String[] getColnames(String tablename){ Cursor c = db.rawquery("SELECT * FROM " + tablename , null); return c.getColumnnames(); } /** Returns the number of rows in tablename **/ public int getCols(String tablename){ return getColnames(tablename).length; } /*** Other methods that have no relevance here .... ***/private static class DBOpenHelper extends sqliteOpenHelper { DBOpenHelper(Context context, String dbname, int dbVersion) { super(context, dbname, null, dbVersion); } @OverrIDe public voID onCreate(sqliteDatabase arg0) { // Do nothing } @OverrIDe public voID onUpgrade(sqliteDatabase arg0, int arg1, int arg2) { // Do nothing } }}
我提供了一些System.out.println()语句来帮助我调试代码并查找问题,因此请忽略它们. PrintDB是我编写的另一项测试,用于确保一切正常.现在,这就是我为确保一切正常而写的内容.
DBAssistant db = new DBAssistant();String dbname = "testing";String tablename = "test";String[] cols = {"_num", "num_eng", "num_span"};String[] row1 = {"1", "one", "uno"};String[] row2 = {"2", "two", "dos"};String[] row3 = {"3", "three", "tres"};String[] row4 = {"4", "four", "quatro"};String[] row5 = {"5", "five", "cinco"};TextVIEw databaseVIEw = (TextVIEw)findVIEwByID(R.ID.databaseVIEw);db.openDB(this, dbname);db.createtable(tablename, cols);db.insertRow(tablename, row1);db.insertRow(tablename, row2);db.insertRow(tablename, row3);db.insertRow(tablename, row4);db.insertRow(tablename, row5);databaseVIEw.setText(db.printtable(tablename));
运行此代码,一切都会顺利进行,所有System.out.println()语句中都包含正确的信息,直到到达databaseVIEw.setText(db.printtable(tablename));为止.
部分并引发异常
ERROR/AndroIDRuntime(3440): androID.database.sqlite.sqliteException: no such table: test: , while compiling: SELECT * FROM test
指向printtable()方法中的行:
Cursor c = dbr.rawquery("SELECT * FROM " + tablename , null);
这让我感到困惑,因为在此之前多次调用getColnames()方法中使用了同一行代码,并且没有问题.另外,如果该表不存在,当我调用insertRow()时会不会引发异常?我连续五次调用该方法,没有抛出任何异常!这是怎么回事
编辑:
Implement onCreate() in your DBOpenHelper. Put there content of the createtable() method.
为什么需要在onCreate()方法中创建表?如果要执行此 *** 作,是否可以使用现有方法来执行此 *** 作,而仅从该方法中调用它?
And the second why you don’t use content provIDers and uri >concept which is already sqlless?
哇…?我不知道.请解释一下这是什么,或者给我留下某种教程的链接.
解决方法:
在您的DBOpenHelper中实现onCreate().将createtable()方法的内容放在此处.
参考Dev Guide.
总结以上是内存溢出为你收集整理的Android:Sqlite Exception没有这样的表?全部内容,希望文章能够帮你解决Android:Sqlite Exception没有这样的表?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)