将子字符串转换为整数的奇怪问题
我在子串处理方面遇到了一个奇怪的问题。显然,由于某些奇怪的原因,我得到的字符串无法转换为 Int32。当我尝试这样做时收到的错误消息是“输入字符串的格式不正确”。因此,我也无法将这些值插入数据库。
这是代码...
string width = GetMetadata(filename, 162); //returns "1280 pixels"
string height = GetMetadata(filename, 164); //returns "700 pixels"
width = width.Substring(0, width.IndexOf(' ')); //returns "1280"
height = height.Substring(0, height.IndexOf(' ')); //returns "700"
//test: "System.Convert.ToInt32(width)" will fail, giving error "input string was not in correct format"
//doing the above on "width" yields the same result
//fails, giving error "no such column: 1280" (underlying database is sqlite)
Database.NonQuery("INSERT INTO image VALUES (" + fileid + ", " + width + ", " + height + ")");
I'm getting a weird issue with substringing. Apparently the string I get can't be cast into an Int32 for some odd reason. The error message I get when I try doing that is "input string is not in correct format". Because of this, I can't insert these values into the Database either.
Here's the code...
string width = GetMetadata(filename, 162); //returns "1280 pixels"
string height = GetMetadata(filename, 164); //returns "700 pixels"
width = width.Substring(0, width.IndexOf(' ')); //returns "1280"
height = height.Substring(0, height.IndexOf(' ')); //returns "700"
//test: "System.Convert.ToInt32(width)" will fail, giving error "input string was not in correct format"
//doing the above on "width" yields the same result
//fails, giving error "no such column: 1280" (underlying database is sqlite)
Database.NonQuery("INSERT INTO image VALUES (" + fileid + ", " + width + ", " + height + ")");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
出于所有正常原因(主要是避免将数据转换留给数据库,并防止 SQL 注入攻击),我建议您在 C# 中对数字进行解析,然后使用 参数化查询与 SQLite 对话。
在这种情况下,这将使调试变得更加容易 - 要么 .NET 也无法解析该字符串(在这种情况下,它可能是您的数据的问题),要么它会工作,并且您不需要担心数据库正在执行什么转换。
编辑:我刚刚看到您的评论说
Convert.ToInt32
也失败了。这非常清楚地表明是数据引起了问题。我希望您的代码看起来像这样:
请注意,Trim 调用将删除所有前导空格,这似乎是问题的原因。
For all the normal reasons - primarily avoiding leaving data conversions to the database, and preventing SQL injection attacks - I would suggest that you perform the parsing to a number in C#, and then use a parameterized query to talk to SQLite.
In this case, that will make it a lot easier to debug - either .NET will fail to parse the string as well (in which case it's likely to be a problem with your data) or it will work, and you won't need to worry about what conversions database was performing.
EDIT: I've just seen your comment saying that
Convert.ToInt32
fails as well. That's a pretty clear indication that it's the data which is causing a problem.I'd expect your code to look something like this:
Note that the
Trim
call will remove any leading spaces, which it seems was the cause of the problem.字符串变量
width
和height
中可能存在一些杂散空格。在将字符串转换为整数之前调用Trim()
方法:希望这会有所帮助。让我们知道。
There may be some stray whitespaces in the string variables
width
andheight
. InvokeTrim()
method on the strings before casting them into integers:Hope this helps. Let us know.