如何使用SQLite JDBC传递文本或字符串?

时间:2013-05-10 14:27:21

标签: sqlite jdbc

我在使用JDBC时遇到此问题。 我可以创建一个名为food.db的表,其中包含早餐,午餐和晚餐。当我打电话给以下时......

          statement.executeUpdate("create table food (breakfast string, lunch string, dinner string)");
          breakfast = JOptionPane.showInputDialog("What was your breakfast?");
            lunch = JOptionPane.showInputDialog("What was your lunch?");
            dinner = JOptionPane.showInputDialog("What was your dinner?");
         statement.executeUpdate("insert into food values(\'"+breakfast+"\', \'"+lunch+"\' ,\'"+dinner+"\')");

但是,最后一句话会导致错误。无论出于何种原因,它表示无论我输入的“早餐”(例如,燕麦片)都不是专栏,即使我知道我可以用这种方式使用SQLite的语法来更新列。

此外,我已经检查了executeUpdate()的参数,并且单引号和所有内容的语法都匹配...我已经尝试了文本和字符串列字段,两者都得到相同的错误。

2 个答案:

答案 0 :(得分:2)

试试这个

string更改为VARCHAR(SIZE)TEXT

 statement.executeUpdate("CREATE TABLE food (breakfast VARCHAR(25), lunch VARCHAR(25), dinner VARCHAR(25))");
 String breakfast = JOptionPane.showInputDialog("What was your breakfast?");
 String lunch = JOptionPane.showInputDialog("What was your lunch?");
 String dinner = JOptionPane.showInputDialog("What was your dinner?");
 statement.executeUpdate("INSERT INTO food (breakfast, lunch , dinner) VALUES ('"+breakfast+"', '"+lunch+"' ,'"+dinner+"')");

答案 1 :(得分:0)

将您的CREATE TABLE更改为

statement.executeUpdate("create table food (breakfast text, lunch text, dinner text)");

您的INSERT INTO语句正在转发单'个引号,这不是必需的。改为

statement.executeUpdate("insert into food values('"+breakfast+"', '"+lunch+"' ,'"+dinner+"')");


SQLite中没有string数据类型,但它没有出错,因为SQLite的SQL没有强类型。列关联性接管并将任何未知数据类型视为NUMERIC。最好将列数据类型切换为TEXT或VARCHAR。