sqlite3_column_text returns NULL when there is data in the database - iphone-sdk-3.0

My database is defined as the following
CREATE TABLE [problems] (
[p_id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
[problem_name] VARCHAR(255) NOT NULL,
[mixed_t1] INT NOT NULL,
[mixed_t2] INT NOT NULL,
[mixed_flair] INT,
[mixed_gre] INT,
[mixed_diffusion] INT,
[mixed_adc] INT,
[fat_sat_post_t1] INT)
In my code, I'm trying to run an SQL query that looks like
SELECT problem_name from problems WHERE mixed_t1=0
Then in code, I'm trying to assign the problem_name to a NSString and then add the NSString to an NUSMutableArray
if(sqlite3_step(statement) == SQLITE_ROW)
{
NSString *problemName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)];
[dataSet addObject:problemName];
[problemName release];
}
The problem is that I'm crashing at NSString *problemName
I get the following crash message
Current language: auto; currently objective-c
2011-02-08 08:54:29.035 RadAppz[1332:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
* Call stack at first throw:
I know I'm getting data because executing the RAW SQL in Terminal returns 2 rows. Can someone please tell me why the data is being returned as NULL?

Your problem, I believe, is that you are checking for the second column, not the first. Note that the definition for sqlite3_column_text states that the leftmost column is column 0.
http://www.sqlite.org/capi3ref.html#sqlite3_column_blob

Related

SQL invalid column name Site.LocationLat,

CREATE TABLE #Data(
[LocationLat] float NULL,
[LocationLong] float NULL,
[LocationHeight] float NULL,
When I am creating table and insert data that time error occurs.
Invalid Column name.
INSERT INTO #Data
SELECT #ServerName,
Site.LocationLat, /*Error occur invalid column name */
Site.LocationLong, /*Error occur invalid column name */
Site.LocationHeight, /*Error occur invalid column name */
If you are using SQL server try to wrap your column names with [].
INSERT INTO #Data
SELECT #ServerName, [Site.LocationLat], [Site.LocationLong], [Site.LocationHeight]
Site doesn't mean anything without a FROM clause. Perhaps you intend something like this:
CREATE TABLE #Data (
SiteName varchar(255),
[LocationLat] float NULL,
[LocationLong] float NULL,
[LocationHeight] float NULL
);
INSERT INTO #Data (SiteName, LocationLat, LocationLong, LocationLong)
SELECT #ServerName, s.LocationLat, s.LocationLong, s.LocationHeight,
FROM Site s;
This assumes you have a table called Site with the appropriate columns.
Please try out the following code
CREATE TABLE #Data (
SiteName varchar(255),
[LocationLat] float NULL,
[LocationLong] float NULL,
[LocationHeight] float NULL );
**
INSERT INTO #Data (SiteName, LocationLat, LocationLong,
LocationHeight)
SELECT ##ServerName, s.LocationLat, s.LocationLong, s.LocationHeight,
FROM Site s;
**
The problem is not with your #Data table it's your Site table. So we really need to see the definition for that. I suspect it doesn't have a LocationLat column.

Conversion failed when converting the varchar value '0%' to data type int

I'm facing a problem with SQL Server.
I've created a table like this:
create table Components
(
pk BIGINT NOT NULL PRIMARY KEY IDENTITY,
id VARCHAR(50),
descr VARCHAR(50),
in_m INT,
in_iy INT,
p_fw VARCHAR(5000)
);
and I'm trying to insert a couple of values:
insert into Components (id, descr, in_m, in_iy, p_fw)
values ('FW000_A', '0%', 0, 0, '[0.0,0.0,0.0]'),
('FW000_B', '1%', 1, 1, '[1.0,1.0,1.0]');
I get the following error:
Msg 245, Level 16, State 1, Line 111
Conversion failed when converting the varchar value '0%' to data type int.
even though the column descr is correctly defined as varchar(50).
Can anybody help me please? Why is SQL Server trying to convert my strings to int values?
What's missing from your question is that you have more than just the two values lines you've shown, and one of the other ones has an integer literal for the descr column, rather than a string.
This example produces the same error:
declare #t table (Descr varchar(50) not null)
insert into #t(Descr) values
('0%'),
(12)
What I believe happens is that SQL Server first tries to determine the data types for all columns in the values clause. Using data type precedence rules, it observes a varchar literal in one row and an int literal in the other, and so determines that the overall type for this column is int and attempts to perform the conversion that leads to the error.
During this process, it does not use any information about the target table into which the values are going to be placed, including the data types of the columns there.
run this and verify the data types;
sp_columns Components
Looks like 'descr' is really an integer

Why am i getting this error when am creating a table?

I am creating a new table, with various data type, one them in binary(n). When I execute the statement, I end up with the following
Implicit conversion from data type varchar to binary is not allowed, use CONVERT function.
My question: why do I get this error, when there is no data to convert, I am just creating a table?
Create Table EMP.DETAILS
(
ID INT NOT NULL,
TYPE INT,
Created datetime2,
Key_No varchar(5),
Batch_IN INT,
UN_ID BINARY(1) not null default '',
Source INT,
SITE CHAR(1)
)
When I execute the above statement, I get the error mentioned above, But it directs me to the CREATE TABLE line of the code.
The error says "Implicit conversion from data type varchar to binary is not allowed". You are trying to assign a default varchar character to a binary here:
UN_ID BINARY(1) not null default ''
If you absolutely need a default value for a binary column, you can set it directly as something like 0x00:
UN_ID BINARY(1) not null default 0x00
That will basically set your default to 0. You can also set it to an integer value:
UN_ID BINARY(1) not null default 0
And finally, if you absolutely need to to be an empty string, you can find the binary representation of '' with the following SELECT:
SELECT CONVERT(binary, '')
I'm pretty sure it's just 0, though.
Depending on what that column is being used for, though, it might be better suited as a tinyint, char(1), or something similar, rather than a binary(1).

INSERT statement sqlite3 objective-c

Hopefully my question has a simple solution. I am just starting to use sqlite3 and I have created a database called local and a table called table with 1 column called name with type text.
It connects to the database fine its just I am trying to input a string value into the tables column but I think there is an issue with my INSERT text. I know its probably something simple if anyone could just let me know where I went wrong. Thanks.
SString *inserStmt = [NSString stringWithFormat:#"INSERT INTO TABLE(NAME) VALUES ('%s')", [key UTF8String]];
const char *insert_stmt = [inserStmt UTF8String];
sqlite3_exec(database, insert_stmt, NULL, NULL, NULL);
Your first problem is that you ignore any errors and don't display the error message, so if something goes wrong, you will have no clue.
Your second problem is that TABLE is a keyword;
to use it as a name in SQL statements, you have to escape it with double quotes:
INSERT INTO "TABLE"(NAME) VALUES('...')

How do I get a NULL into this SQLite insert statement?

I have 3 SQLite tables. Each has an ID as primary key; if I provide a NULL when inserting a record to ID, it will auto-increment the key (at least that's what the SQLite docs say). I am trying to get the NULL into this insert statement, but it crashes ('NSInvalidArgumentException', reason: '* -[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'
). Here is a snippet of the statement (dbCmd is defined as NSString):
dbCmd = #"INSERT INTO CustData (ID, BUS_NAME, EMAIL, PHONE, SHOP_NAME, SHOP_ADDR1, SHOP_ADDR2, SHOP_CITY_STATE, SHOP_ZIP, "
"SHIP_NAME, SHIP_ADDR1, SHIP_ADDR2, SHIP_CITY_STATE, SHIP_ZIP, SALES_NAME, NOTES) VALUES('";
dbCmd = [dbCmd stringByAppendingFormat:NULL]; // <--------------------
dbCmd = [dbCmd stringByAppendingString: [methodParameters objectForKey: #"businessName"]];
dbCmd = [dbCmd stringByAppendingString: #"', '"];
dbCmd = [dbCmd stringByAppendingString: [methodParameters objectForKey: #"email"]];
How do I fix this?
From the sqlite docs:
If no ROWID is specified on the insert, or if the specified ROWID has a value of NULL, then an appropriate ROWID is created automatically.
http://www.sqlite.org/autoinc.html
If you have no specific reason to specify the ID, you can simply leave it out!