我正在尝试为类项目构建一个简单的库管理系统。我正在使用SQLite来做到这一点。 该应用程序如下所示:
首先,我从表格中选择并加载所有内容,如上图所示,其工作正常:
这是创建表查询:
"CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, isbn TEXT, category TEXT, authors TEXT, publisher TEXT, edition INTEGER, copies INTEGER, pdf TEXT, cover TEXT)";
以下是选择整个表格的查询:
"SELECT id, title, isbn, authors, publisher, edition, copies, pdf, cover FROM books ORDER BY id";
为了实现搜索功能,我尝试使用' LIKE'条款,像这样:
"SELECT id, title, isbn, category, authors, publisher, edition, copies, pdf, cover FROM books WHERE title LIKE %'"+userInput+"'% ORDER BY id";
当我使用上述查询进行搜索时,出现此错误:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at (address-removed)/BooksContainer.as:124]
这是第124行:
var numResults:uint = result.data.length;
我尝试更改代码但在使用了大约一周后,我仍然遇到同样的错误。 昨天,然而我试图以不同的方式处理它,因为我真的很沮丧,我确信这种方法会起作用。我做的是我选择了整个表格如下:
public function searchBooks(userInput:String)
{
var stat:SQLStatement = new SQLStatement();
stat.sqlConnection = _connection;
stat.text = "SELECT id, title, isbn, category, authors, publisher, edition, copies, pdf, cover FROM books ORDER BY id";
//stat.parameters['@search'] = "%" + userInput + "%";
stat.execute();
trace(stat.text);
searchedString = userInput;
stat.addEventListener(SQLEvent.RESULT, createSearchedContent, false, 0, true);
}
private function createSearchedContent(e:SQLEvent):void
{
var result:SQLResult = e.target.getResult();
_allBooks = result;
trace(result.data);
var numResults:uint = result.data.length;
var currentY:Number = 0;
var xIterator:Number = 0;
for (var i:int = 0; i < numResults; i++)
{
var row:Object = result.data[i];
var bookTitle:String = String(result.data[i].title);
if (bookTitle.toLowerCase().indexOf(searchedString.toLowerCase()) > -1)
{
var book:Book = new Book(row.title, row.isbn, row.category, row.authors, row.publisher, row.edition, row.copies, row.pdf, row.cover);
book.x = book.width * xIterator + 10;
book.y = currentY;
addChild(book);
xIterator++;
if (xIterator == 7)
{
xIterator = 0;
currentY += book.height + 10;
}
}
}
}
令人惊讶的是,我得到了同样的错误。我根本没有得到它。然后我把它放在上面的循环中:
var a:String = _main.search_txt.text.toLowerCase();
var b:String = String(row.title.toLowerCase());
trace(b.indexOf(a), a, b);
我发现的是,在追踪中我总是得到-1来搜索这个词。
这是输出窗口:
-1 data dark matter and the dinosaures
-1 data a history of religious ideas
-1 data steve jobs
-1 data digital logic design
-1 data thomas calculus
-1 data Data structures and algorithms in java
-1 data data structures in c++
-1 data data structures and algorithms in java
-1 data data structures and algorithms in java
那么任何人都可以帮助我吗?我只有一个星期的时间来完成这个项目。提前谢谢。
答案 0 :(得分:1)
看起来百分比运营商定位问题。
收集查询
where something like '%foo%'
您的查询
where something like %'foo'%
将百分比运算符放在单引号中。
"SELECT id, title, isbn, category, authors, publisher, edition, copies, pdf, cover FROM books WHERE title LIKE '%"+userInput+"%' ORDER BY id";
你也可以在参数中使用like子句 ActionScript and SQLite parameters on Select using Like