SQLiteException - 没有这样的表

时间:2011-05-25 20:39:43

标签: android sqlite

当我尝试访问我的视图时出现此错误

我使用此

构建了我的数据库/ View
CREATE TABLE Boxer(
BoxerId INTEGER PRIMARY KEY AUTOINCREMENT,
Firstname NVARCHAR(50) NOT NULL,
Lastname NVARCHAR(50) NOT NULL
);

CREATE TABLE Match(
MatchId INTEGER PRIMARY KEY AUTOINCREMENT,
BoxerA INTEGER NOT NULL FOREIGN KEY REFERENCES Boxer(BoxerId),
BoxerB INTEGER NOT NULL FOREIGN KEY REFERENCES Boxer(BoxerId),
MatchDate date NOT NULL DEFAULT GETDATE(),
NumberOfRounds INTEGER NOT NULL DEFAULT  12
);

CREATE TABLE Round(
RoundId INTEGER PRIMARY KEY AUTOINCREMENT,
MatchId INTEGER NOT NULL FOREIGN KEY REFERENCES Match(MatchId),
BoxerA INTEGER NOT NULL DEFAULT 0,
BoxerB INTEGER NOT NULL DEFAULT 0,
Position INTEGER NOT NULL
);

/*
Building a view which dislpays matches with boxers names and total scores
*/
CREATE VIEW MatchDetail AS
SELECT Match.MatchId, A.BoxerId AS IdA, B.BoxerId AS IdB, A.Firstname + ' ' + A.Lastname         AS NameA, B.Firstname + ' ' + B.Lastname AS NameB,
(SELECT SUM(R.BoxerA) AS Score FROM Round AS R WHERE (R.MatchId = Match.MatchId)) AS ScoreA,
(SELECT SUM(R.BoxerB) AS Score FROM Round AS R WHERE (R.MatchId = Match.MatchId)) AS ScoreB, 
Match.MatchDate, Match.NumberOfRounds
FROM Boxer AS A INNER JOIN Match ON A.BoxerId = Match.BoxerA INNER JOIN Boxer AS B ON     Match.BoxerB = B.BoxerId

到目前为止,我已经使用记事本示例构建了我的应用程序,因此我调用了我的DbHelper

Cursor MatchesCursor = mDbHelper.fetchAllMatchDetails();

然后调用查询

public Cursor fetchAllMatchDetails(){
    return mDb.query(VIEW_MATCHDETAIL, new String[] {
            "MatchId"
            }, null, null, null, null, null);
}

VIEW_MATCHDETAIL被定义为string =“MatchDetail”

它就在这里崩溃说

  

没有这样的表MatchDetail:在编译SELECT MatchId FROM MatchDetail

之前有人遇到过这个问题吗?

3 个答案:

答案 0 :(得分:2)

你有一些漂亮的SQL。不幸的是,只有第一行的SQL将在SQLiteDatabase.execSQL中执行。其余的将被默默忽略(方便吗?)。像这样手动拆分语句:

https://github.com/browep/fpt/blob/master/src/com/github/browep/nosql/NoSqlSqliteOpener.java

或者如果你想将你的sql保存在一个单独的文件中,试试这个:

String sqlText = getSqlText();
for(String sqlStmt : sqlText.split(";"))
     myDb.execSQL(slqStmt + ";");

答案 1 :(得分:0)

对我来说最突出的是使用像NVARCHAR(50)这样的数据类型。 SQLite只有very simple set of datatypes。我很惊讶它在您安装应用程序时不会抛出异常。请尝试使用TEXT代替。

答案 2 :(得分:0)

如果您无法访问已知已初始化的数据库,请尝试将创建该表的Activity中的Context传递给尝试查询该表的类。将该Context用作连接初始化的一部分。