在Windows7里,用Java7和sqlite3,想写一个图书管理程序,但是使用sqlite的like查询语句没有查到相关的结果,而在命令行下用sqlite3.exe命令执行相同的查询语句却有结果。详细代码如下:
Java里面的相关代码:
- Java code
//用书名查询书籍信息 private void QueryByName()throws Exception { // TODO Auto-generated method stub String name; System.out.print("请输入要查找的书名:"); Scanner scanner = new Scanner(System.in); name = scanner.next(); String querySql = "select book.BookID,book.BookName,book.BookPage,author.Author,author.Translater,type.TypeName" + " from book,author,type where book.BookName like \"%" + name + "%\" and author.BookID = book.BookID and " + "book.TypeID = type.TypeID;"; System.out.println(querySql); System.in.read(); ResultSet rs = null; DataBase database = null; try { database = new DataBase(); database.InitDatabase("book.db"); rs = database.RunQuery(querySql); if (!rs.next()) { System.out.println("没有结果......"); System.in.read(); } int n = 0; while (rs.next()) { System.out.println("书籍ID:" + rs.getInt("BookID")); System.out.println("书籍名称:" + rs.getString("BookName")); System.out.println("书籍页数:" + rs.getInt("BookPage")); System.out.println("作者:" + rs.getString("Author")); System.out.println("译者:" + rs.getString("Translater")); System.out.println("书籍分类:" + rs.getString("TypeName")); n++; } System.out.println("共有" + n + "项结果。"); System.out.println("查询结果输出完毕,按回车键继续......"); System.in.read(); System.in.read(); } catch (Exception e) { System.out.println("查询书籍信息出错!详细信息:" + e.getMessage() + "请按回车键继续......"); System.in.read(); System.in.read(); return; } finally { if (rs != null) { rs.close(); } database.CloseDatabase(); } }
其中RunQuery是另一个类里的成员函数
- Java code
public ResultSet RunQuery(String sql)throws Exception{ ResultSet rs; rs = state.executeQuery(sql); return rs;}
程序的插入数据的功能已经完成,并通过该功能插入了一些测试数据,下面是在Windows7命令行下执行sqlite3.exe book.db的结果:
- SQL code
sqlite> .schemaCREATE TABLE [author] ([BookID] INTEGER PRIMARY KEY,[Translater] VARCHAR(100),[Author] VARCHAR(100) not null);CREATE TABLE [book] ([BookID] INTEGER primary key autoincrement,[BookName] VARCHAR(100) not null,[BookPage] INTEGER not null,[TypeID] INTEGER not null);CREATE TABLE [type] ([TypeID] INTEGER PRIMARY KEY,[TypeName] VARCHAR(100) not null);sqlite> select * from book;2|sdf|34|3423|sdf|4334|4344|dsfds|344|54532|sdfs|43|434345|鍝堝搱|324|34282324|娴嬭瘯|3543|2342353324|浣犲ソ|323|344323
在命令行下就是有乱码,不是复制上来才有的。
下面执行最前面贴出的那段JAVA代码的功能:
- Java code
请输入要查找的书名:sdfselect book.BookID,book.BookName,book.BookPage,author.Author,author.Translater,type.TypeName from book,author,type where book.BookName like "%sdf%" and author.BookID = book.BookID and book.TypeID = type.TypeID;共有0项结果。查询结果输出完毕,按回车键继续......