0

我似乎无法做到这一点,所以我问我在代码中忽略了什么。这是我的代码中的内容:

newLeaseData = "INSERT INTO " + enterLogLN + " (Date, "
            + "StockTank1Ft, StockTank1Inch, StockTank2Ft, StockTank2Inch, "
            + "StockTank3Ft, StockTank3Inch, StockTank4Ft, StockTank4Inch, "
            + "Change1, Change2, Change3, Change4) VALUES " 
            + "(" + today + ", '" + valueT1Ft + "', '" + valueT1Inch + "', '" + valueT2Ft 
            + "', '" + valueT2Inch + "', '" + valueT3Ft + "', '" + valueT3Inch 
            + "', '" + valueT4Ft + "', '" + valueT4Inch + "', " + difTotalT1 
            + ", " + difTotalT2 + ", " + difTotalT3 + ", " + difTotalT4 + ")";

一切正常,直到我得到字符串:difTotalT1、difTotalT2、difTotalT3 和 difTotalT4。每个字符串都设置为: 0'-1" 、 0'-2" 、 0'-3" 或 0'-4" ,当我尝试执行命令时:

myDataBase.execSQL(EnterLogDataOilActivity.newLeaseData);

我的应用程序因此 logcat 错误而崩溃:

06-05 04:36:23.824: E/AndroidRuntime(1127): android.database.sqlite.SQLiteException: "'-1", 0'" 附近: 语法错误: INSERT INTO about (Date, StockTank1Ft, StockTank1Inch, StockTank2Ft, StockTank2Inch,StockTank3Ft,StockTank3Inch,StockTank4Ft,StockTank4Inch,Change1,Change2,Change3,Change4)值('05_05_12','0','1','0','2','0','3','0' ', '4', 0'-1", 0'-2", 0'-3", 0'-4")

感谢您的帮助,我知道它必须很简单。我也尝试用单引号括住每个 difTotalT1 字符串,但这也不起作用(即 difTotalT1 = "'" + difTotalT1 + "'";)

这是我用来在 sqlite 中创建表的字符串:

newLeaseTable = "create table '" + leaseName + "' (_id integer primary key autoincrement,"
            + " Date TEXT, StockTank1Ft NUMERIC, StockTank1Inch NUMERIC,"
            + " StockTank2Ft NUMERIC, StockTank2Inch NUMERIC, StockTank3Ft NUMERIC,"
            + " StockTank3Inch NUMERIC, StockTank4Ft NUMERIC, StockTank4Inch NUMERIC,"
            + " Change1 TEXT, Change2 TEXT, Change3 TEXT, Change4 TEXT, User TEXT);";
4

3 回答 3

0

尝试以下方式,

String singleQuote = "'";
char doubleQuote = '"';

现在将您的变量与上述变量连接起来,如下所示

String Change3 = "0" + singleQuote + "-3" + doubleQuote;  // result = 0'-3" in variable
于 2012-06-05T04:47:23.753 回答
0

你必须用反斜杠转义引号,即

difTotal = "0\\\'-1\\\"";

当您使用它插入 MySQL 时,这会导致 diffTotal 为“0\'-1\””。

于 2012-06-05T04:51:07.397 回答
0

我无法让它工作,所以我将我的 var 从一个字符串更改为一个 int,然后对其进行设置,以便它以不同的方式显示它。感谢您的帮助

于 2012-06-06T03:03:48.753 回答