SQLite error: 'could not convert text value to numeric value.' - sqlite

I've found a work-around, but I am completely perplexed by an issue I ran into with Adobe Air and SQLite. An UPDATE query that I believe worked correctly earlier in the development process suddenly started failing with the error details:'could not convert text value to numeric value.', operation:'execute', detailID:'2300'. Several hours later I found that if I included a pretty irrelevant column in the query, setting it to an arbitrary value, the problem went away. As luck would have it, this did not affect the business logic in any meaningful way, so I'm going to leave it as is. However, I detest mysteries like this. Can anyone think of what might have happened?
(Edit: Sorry, I made a mistake in my code. The last two queries were identical in my original post. In fact, the UPDATE query only worked when I also added locked = null or locked = 0 to the query. If I didn't set this value, the query would fail. I did just about everything I could to get around this, including rebuilding the database file.)
Here's the table definition:
CREATE TABLE cars (
car_id INTEGER PRIMARY KEY AUTOINCREMENT DEFAULT NULL,
cargroup_id NUMERIC,
starting_ordinal NUMERIC,
ending_ordinal NUMERIC,
locked NUMERIC
);
This query has always worked:
var query = new Query(
"UPDATE cars SET locked = (car_id = ?) WHERE cargroup_id = ?",
[intCarId,intCargroupId],
success,failure
);
This query failed with the above error ([edit]):
var query = new Query(
"UPDATE cars SET starting_ordinal = ?, ending_ordinal = ?, cargroup_id = ? WHERE car_id = ?",
[
parseInt(objPayout.starting_ordinal,10),
parseInt(objPayout.ending_ordinal,10),
parseInt(objPayout.cargroup_id,10),
parseInt(objPayout.car_id,10)
],
success,failure
);
I solved the problem by changing the query to this:
var query = new Query(
"UPDATE cars SET starting_ordinal = ?, ending_ordinal = ?, cargroup_id = ?, locked = null WHERE car_id = ?",
[
parseInt(objPayout.starting_ordinal,10),
parseInt(objPayout.ending_ordinal,10),
parseInt(objPayout.cargroup_id,10),
parseInt(objPayout.car_id,10)
],
success,failure
);

Related

Creating a new table in sqlite database [duplicate]

I'm having some strange feeling abour sqlite3 parameters that I would like to expose to you.
This is my query and the fail message :
#query
'SELECT id FROM ? WHERE key = ? AND (userid = '0' OR userid = ?) ORDER BY userid DESC LIMIT 1;'
#error message, fails when calling sqlite3_prepare()
error: 'near "?": syntax error'
In my code it looks like:
// Query is a helper class, at creation it does an sqlite3_preprare()
Query q("SELECT id FROM ? WHERE key = ? AND (userid = 0 OR userid = ?) ORDER BY userid DESC LIMIT 1;");
// bind arguments
q.bindString(1, _db_name.c_str() ); // class member, the table name
q.bindString(2, key.c_str()); // function argument (std::string)
q.bindInt (3, currentID); // function argument (int)
q.execute();
I have the feeling that I can't use sqlite parameters for the table name, but I can't find the confirmation in the Sqlite3 C API.
Do you know what's wrong with my query?
Do I have to pre-process my SQL statement to include the table name before preparing the query?
Ooookay, should have looked more thoroughly on SO.
Answers:
- SQLite Parameters - Not allowing tablename as parameter
- Variable table name in sqlite
They are meant for Python, but I guess the same applies for C++.
tl;dr:
You can't pass the table name as a parameter.
If anyone have a link in the SQLite documentation where I have the confirmation of this, I'll gladly accept the answer.
I know this is super old already but since your query is just a string you can always append the table name like this in C++:
std::string queryString = "SELECT id FROM " + std::string(_db_name);
or in objective-C:
[#"SELECT id FROM " stringByAppendingString:_db_name];

SELECT CASE - THEN - WHEN query throws an error [duplicate]

My code follows:
SELECT COUNT(_id) AS count FROM general WHERE _id = 1 CASE WHEN count > 0 THEN UPDATE general SET userGivenId = 'xxx' WHERE _id = 1 ELSE INSERT INTO general (userGivenId) VALUES ('xxx' ) END
With the error:
android.database.sqlite.SQLiteException: near "CASE": syntax error: , while compiling: SELECT COUNT(_id) AS count FROM general WHERE _id = 1 CASE WHEN count > 0 THEN UPDATE general SET userGivenId = 'xxx' WHERE _id = 1 ELSE INSERT INTO general (userGivenId) VALUES ('xxx' ) END
This is the shortest query I will use. Why I do this is because my other queries will have rows that needs to be updated but some may not be touched. Using replace will replace all the data (at least that is how it works for me on my Android phone). For instance my File class will have a filePath, but sometimes the response from the server will return null and I am only to update the File IF the server returns a new File.
Did I forget to write anything?
SQLite does not have any control logic because this would not make sense in an embedded database (there is no separate server machine/process whose asynchronous execution could improve performance).
CASE can be used only for expressions, not for entire commands.
Handle the control logic in your app:
Cursor c = db.rawQuery("SELECT 1 FROM general WHERE _id = 1", null);
if (c.moveToFirst())
db.execSQL("UPDATE general SET userGivenId = 'xxx' WHERE _id = 1");
else
db.execSQL("INSERT INTO general (userGivenId) VALUES ('xxx')");
For these particular commands, if you have a unique constraint on the _id column (and a primary key is constrained to be unique), you can use the INSERT OR REPLACE command:
INSERT OR REPLACE INTO general(_id, userGivenId) VALUES(1, 'xxx')

Why are null values fetched from ADO Recordset instead of correct ones depending on the order of retrieval? (The DB table has NTEXT values)

I have a DB table with two datetime nullable columns that I need to read from an ASP page using VBScript.
This is the code I wrote:
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
.ActiveConnection = conn
.CommandType = adCmdText
.Prepared = True
.CommandText = "SELECT * FROM storico_corsi WHERE stc_id = 5 "
Set rs = .Execute
Response.Write("stc_scadenza = {" & rs("stc_scadenza") & "}, ")
Response.Write("stc_inizio = {" & rs("stc_inizio") & "} ")
If IsEmpty(rs("stc_inizio")) Then
Response.Write("- ERROR!")
End If
End With
This code gives this output:
stc_scadenza = {19/04/2014}, stc_inizio = {} - ERROR!
If I swap the order of retrieval to
...
Response.Write("stc_inizio = {" & rs("stc_inizio") & "}, ")
Response.Write("stc_scadenza = {" & rs("stc_scadenza") & "} ")
...
this is the (correct) result I get:
stc_inizio = {19/02/2014}, stc_scadenza = {19/04/2014}
Why is a small change in the order of retrieval of elements from an ADO Recordset delivering a completely different outcome?
Please notice that I'm using the Italian locale (dd/mm/yyyy) and that stc_inizio and stc_scadenza, while being often set to midnight timestamps, are of SQL type datetime.
UPDATE #1: I made the code simpler and clearer by reducing the code to the manipulation of just two fields, adding the emptiness check and removing JSON stuff altogether. Some comments below refer to previous, more complicated, versions.
UPDATE #2: If I replace the SQL query with
SELECT stc_inizio, stc_scadenza FROM storico_corsi WHERE stc_id = 5
or
SELECT stc_scadenza, stc_inizio FROM storico_corsi WHERE stc_id = 5
it works properly! But why? This is the DB table I'm using:
CREATE TABLE [dbo].[storico_corsi] (
[stc_id] [bigint] IDENTITY(1,1) NOT NULL,
[stc_id_ute] [bigint] NOT NULL,
[stc_utente] [varchar](100) NULL,
[stc_anagrafica] [ntext] NULL,
[stc_id_can] [bigint] NULL,
[stc_canale] [varchar](500) NULL,
[stc_FE_id] [bigint] NULL,
[stc_quest_finale] [ntext] NULL,
[stc_quest_corretto] [ntext] NULL,
[stc_reg_fad] [ntext] NULL,
[stc_inizio] [datetime] NULL,
[stc_scadenza] [datetime] NULL,
[stc_terminato] [char](1) NULL
CONSTRAINT [DF_storico_corsi_stc_terminato_1] DEFAULT ('N'),
[stc_fine] [datetime] NULL,
[stc_tempo] [bigint] NULL
CONSTRAINT [DF_storico_corsi_stc_tempo] DEFAULT ((0)),
[stc_data_in] [datetime] NULL
CONSTRAINT [DF_storico_corsi_stc_data_in_1] DEFAULT (getdate()),
[stc_progressivo] [int] NULL,
[stc_anno] [int] NULL,
CONSTRAINT [PK_storico_corsi] PRIMARY KEY CLUSTERED ([stc_id] ASC)
WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)
ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
UPDATE #3: The issue can be reproduced anytime I include any of the 3 ntext columns in the query. If I don't SELECT them, nothing bad happens. Apparently, ntext values break the record, but they also do that in an unpredictable way that depends on the order of retrieval of other fields.
UPDATE #4: BLOBs/ntexts have to be fetched last with ADO. This statement sounds a bit crazy but this is what I found here: http://p2p.wrox.com/sql-server-2000/3211-cant-pull-data-ntext-field-into-recordset.html#post78234 It's consistent with my experience.
Given the results you're seeing as we've investigated further, it seems as though the problem stems from the NTEXT columns that you're selecting implicitly with your SELECT * ... (but not using.)
As you've noted, there are occasional hints on the web that there at least used to be problems retrieving large binary fields earlier than the end of the SELECT list when using ADO. (As soon as you linked to that article I did have a vague memory from the dim and distant past that I'd seen that advice before too.)
I'd suspect you may be using an older version of ADO, and that this problem may have been fixed in later releases.
Given that in this particular case you don't actually want to retrieve the values from the NTEXT columns, you should simply limit your SELECT list to the values you actually need, and everything should work fine.
Note that it's generally considered best practice to avoid the use of SELECT * except for quick, ad-hoc queries. Explicitly selecting the columns you need has several benefits. For example, in this case, even if your SELECT had worked perfectly, you'd still have been retrieving the potentially large amounts of data in your NTEXT fields (possibly across the network) from your database only to discard it without using it...
Note also that in KB article 317016, "How To Read and Write BLOBs Using GetChunk and AppendChunk", there's some "suggestions for using BLOBs with ADO", including:
Select the BLOB columns last. Select individual fields, not "*".

Return an integer if nothing is found (TSQL ASP.NET VB)

I need to return a value (0) if nothing is found in an SQL call.
Here is what i have (edited/simplified to make more sense out of context), this is baing called from the codebehind.
sql1 = "INSERT INTO [Xtr_MenuItems]([menu_order])
values(1 + (select max(menu_order) from [Xtr_MenuItems]))
SO into database i insert the max number found in [menu_order] + 1. this works fine, assuming something is found.
However, if (select max(menu_order) from [Xtr_MenuItems])) fails (nothing found) then i want to return 0 (as 1 + nothing = nothing, and sql explodes)
How can i do this? I have tried 'IF EXISTS', 'OUTPUT' in various ways but cant get it to work...
Try this:
sql1 = "INSERT INTO [Xtr_MenuItems]([menu_order])
values(1 + ISNULL((select max(menu_order) from [Xtr_MenuItems]),0))
I used ISNULL function where if the result of query is null returns 0
Instead of values, you could use select straight away:
insert Xtr_MenuItems
(menu_order)
select 1 + isnull(max(menu_order),0)
from Xtr_MenuItems
COALESCE is the standards compliant way to provides alternatives for null values:
sql1 = "INSERT INTO [Xtr_MenuItems]([menu_order])
values(1 + COALESCE((select max(menu_order) from [Xtr_MenuItems]),0))
It has similar syntax to the ISNULL function provided by Microsoft, but will work across multiple db platforms and can handle multiple fallbacks.
W3Schools privides a great introduction to null handling here.

Strange SQLite behavior: Not returning results on simple queries

Ok, so I have a basic table called "ledger", it contains fields of various types, integers, varchar, etc.
In my program, I used to use a query with no "from" predicate to collect all of the rows, which of course works fine. But... I changed my code to allow selecting one row at a time using "where acctno = x" (where X is the account number I want to select at the time).
I thought this must be a bug in the client library for my programming language, so I tested it in the SQLite command-line client - and it still doesn't work!
I am relatively new to SQLite, but I have been using Oracle, MS SQL Server, etc. for years and never seen this type of issue before.
Other things I can tell you:
* Queries using other integer fields also don't work
* Queries on char fields work
* Querying it as a string (with the account number on quotes) still doesn't work. (I thought maybe the numbers were stored as a string inadvertently).
* Accessing rows by rowid works fine - which is why I can edit the database with GUI tools with no noticeable problem.
Examples:
Query with no WHERE (works fine):
1|0|0|JPY|8|Paid-In Capital|C|X|0|X|0|0||||0|0|0|
0|0|0|JPY|11|Root Account|P|X|0|X|0|0|SYSTEM|20121209|150000|0|0|0|
3|0|0|JPY|13|Mitsubishi Bank Futsuu|A|X|0|X|0|0|SYSTEM|20121209|150000|0|0|0|
4|0|0|JPY|14|Japan Post Bank|A|X|0|X|0|0|SYSTEM|20121209|150000|0|0|0|
...
Query with WHERE clause: (no results)
sqlite> select * from ledger where acctno=1;
sqlite>
putting quotes around the 1 above changes nothing.
Interestingly enough, "select * from ledger where acctno > 1" returns results! However since it returns ALL results, it's not terrible useful.
I'm sure someone will ask about the table structure, so here goes:
sqlite> .schema ledger
CREATE TABLE "LEDGER" (
"ACCTNO" integer(10,0) NOT NULL,
"drbal" integer(20,0) NOT NULL,
"crbal" integer(20,0) NOT NULL,
"CURRKEY" char(3,0) NOT NULL,
"TEXTKEY" integer(10,0),
"TEXT" VARCHAR(64,0),
"ACCTYPECD" CHAR(1,0) NOT NULL,
"ACCSTCD" CHAR(1,0),
"PACCTNO" number(10,0) NOT NULL,
"CATCD" number(10,0),
"TRANSNO" number(10,0) NOT NULL,
"extrefno" number(10,0),
"UPDATEUSER" VARCHAR(32,0),
"UPDATEDATE" text(8,0),
"UPDATETIME" TEXT(6,0),
"PAYEECD" number(10,0) NOT NULL,
"drbal2" number(10,0) NOT NULL,
"crbal2" number(10,0) NOT NULL,
"delind" boolean,
PRIMARY KEY("ACCTNO"),
CONSTRAINT "fk_curr" FOREIGN KEY ("CURRKEY") REFERENCES "CURRENCY" ("CUR
RKEY") ON DELETE RESTRICT ON UPDATE CASCADE
);
The strangest thing is that I have other similar tables where this works fine!
sqlite> select * from journalhdr where transno=13;
13|Test transaction ATM Withdrawel 20130213|20130223||20130223||
TransNo in that table is also integer (10,0) NOT NULL - this is what makes me thing it is something to do with the values.
Another clue is that the sort order seems to be based on ascii, not numeric:
sqlite> select * from ledger order by acctno;
0|0|0|JPY|11|Root Account|P|X|0|X|0|0|SYSTEM|20121209|150000|0|0|0|
1|0|0|JPY|8|Paid-In Capital|C|X|0|X|0|0||||0|0|0|
10|0|0|USD|20|Sallie Mae|L|X|0|X|0|0|SYSTEM|20121209|153900|0|0|0|
21|0|0|USD|21|Skrill|A|X|0|X|0|0|SYSTEM|20121209|154000|0|0|0|
22|0|0|USD|22|AES|L|X|0|X|0|0|SYSTEM|20121209|154200|0|0|0|
23|0|0|JPY|23|Marui|L|X|0|X|0|0|SYSTEM|20121209|154400|0|0|0|
24|0|0|JPY|24|Amex JP|L|X|0|X|0|0|SYSTEM|20121209|154500|0|0|0|
3|0|0|JPY|13|Mitsubishi Bank Futsuu|A|X|0|X|0|0|SYSTEM|20121209|150000|0|0|0|
Of course the sort order on journalhdr (where the select works properly) is numeric.
Solved! (sort-of)
The data can be fixed like this:
sqlite> update ledger set acctno = 23 where rowid = 13;
sqlite> select * from ledger where acctno = 25;
25|0|0|JPY|0|Test|L|X|0|X|0|0|SYSTEM|20130224|132500|0|0|0|
Still, if it was stored as strings, then that leave a few questions:
1. Why couldn't I select it as a string using the quotes?
2. How did it get stored as a string since it is a valid integer?
3. How would you go about detecting this problem normally besides noticing bizzarre symptoms?
Although the data would normally be entered by my program, some of it was created by hand using Navicat, so I assume the problem must lie there.
You are victim of SQLite dynamic typing.
Even though SQLite defines system of type affinity, which sets some rules on how input strings or numbers will be converted to actual internal values, but it does NOT prevent software that is using prepared statements to explicitly set any type (and data value) for the column (and this can be different per row!).
This can be shown by this simple example:
CREATE TABLE ledger (acctno INTEGER, name VARCHAR(16));
INSERT INTO ledger VALUES(1, 'John'); -- INTEGER '1'
INSERT INTO ledger VALUES(2 || X'00', 'Zack'); -- BLOB '2\0'
I have inserted second row not as INTEGER, but as binary string containing embedded zero byte. This reproduces your issue exactly, see this SQLFiddle, step by step. You can also execute these commands in sqlite3, you will get the same result.
Below is Perl script that also reproduces this issue
This script creates just 2 rows with acctno having values of integer 1 for first, and "2\0" for second row. "2\0" means string consisting of 2 bytes: first is digit 2, and second is 0 (zero) byte.
Of course, it is very difficult to visually tell "2\0" from just "2", but this is what script below demonstrates:
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI qw(:sql_types);
my $dbh = DBI->connect("dbi:SQLite:test.db") or die DBI::errstr();
$dbh->do("DROP TABLE IF EXISTS ledger");
$dbh->do("CREATE TABLE ledger (acctno INTEGER, name VARCHAR(16))");
my $sth = $dbh->prepare(
"INSERT INTO ledger (acctno, name) VALUES (?, ?)");
$sth->bind_param(1, "1", SQL_INTEGER);
$sth->bind_param(2, "John");
$sth->execute();
$sth->bind_param(1, "2\0", SQL_BLOB);
$sth->bind_param(2, "Zack");
$sth->execute();
$sth = $dbh->prepare(
"SELECT count(*) FROM ledger WHERE acctno = ?");
$sth->bind_param(1, "1");
$sth->execute();
my ($num1) = $sth->fetchrow_array();
print "Number of rows matching id '1' is $num1\n";
$sth->bind_param(1, "2");
$sth->execute();
my ($num2) = $sth->fetchrow_array();
print "Number of rows matching id '2' is $num2\n";
$sth->bind_param(1, "2\0", SQL_BLOB);
$sth->execute();
my ($num3) = $sth->fetchrow_array();
print "Number of rows matching id '2<0>' is $num3\n";
Output of this script is:
Number of rows matching id '1' is 1
Number of rows matching id '2' is 0
Number of rows matching id '2<0>' is 1
If you were to look at resultant table using any SQLite tool (including sqlite3), it will print 2 for second row - they all get confused by trailing 0 inside a BLOB when it gets coerced to string or number.
Note that I had to use custom param binding to coerce type to BLOB and permit null bytes stored:
$sth->bind_param(1, "2\0", SQL_BLOB);
Long story short, it is either some of your client programs, or some of client tools like Navicat which screwed it up.

Resources