INSERT statement sqlite3 objective-c - 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('...')

Related

Failed to execute query. Error: String or binary data would be truncated in table xdbo.user_info', column 'uid'

I have problem inserting values in my SQL server database on Azure, I am getting the following error:
Failed to execute query. Error: String or binary data would be truncated in table 'dummy_app.dbo.user_info', column 'uid'. Truncated value: 'u'.
The statement has been terminated.
I don't understand where I am wrong, I just created the server, and I am trying to experiment but cant fix this.
if not exists (select * from sysobjects where name='user_info' and xtype='U')
create table user_info (
uid varchar unique,
name varchar,
email varchar
)
go;
INSERT INTO dbo.user_info(uid, name, email) VALUES('uids', 'name', 'email') go;
Creating the table works fine, the only thing that doesn't work is the second command INSERT
I suspect that the reason is that you haven't defined a lenght for varchar and it defaults to 1 as length. Therefore your value gets truncated.
Set a varchar length to something like varchar(200) and you should be good to go.
This looks like the fact that the CREATE portion of your procedure for the table doesn't include a length of varchar, so you'd have to specify a length such as varchar(50) since the default is 1. Refer to the official MS docs in the link, in the remarks.
docs.miscrosoft.com
Also, here is the syntax for the CREATE TABLE in Azure which might be helpful as well.
Syntax of Azure CREATE TABLE

Trigger to convert empty string to 'null' before it posts in SQL Server decimal column

I've got a front table that essentially matches our SSMS database table t_myTable. Some columns I'm having problems with are those with numeric data types in the db. They are set to allow null, but from the front end when the user deletes the numeric value and tries to send a blank value, it's not posting to the database. I suspect because this value is sent back as an empty string "" which does not translate to the null allowable data type.
Is there a trigger I can create to convert these empty strings into null on insert and update to the database? Or, perhaps a trigger would already happen too late in the process and I need to handle this on the front end or API portion instead?
We'll call my table t_myTable and the column myNumericColumn.
I could also be wrong and perhaps this 'empty string' issue is not the source of my problem. But I suspect that it is.
As #DaleBurrell noted, the proper place to handle data validation is in the application layer. You can wrap each of the potentially problematic values in a NULLIF function, which will convert the value to a NULL if an empty string is passed to it.
The syntax would be along these lines:
SELECT
...
,NULLIF(ColumnName, '') AS ColumnName
select nullif(Column1, '') from tablename
SQL Server doesn't allow to convert an empty string to the numeric data type. Hence the trigger is useless in this case, even INSTEAD OF one: SQL Server will check the conversion before inserting.
SELECT CAST('' AS numeric(18,2)) -- Error converting data type varchar to numeric
CREATE TABLE tab1 (col1 numeric(18,2) NULL);
INSERT INTO tab1 (col1) VALUES(''); -- Error converting data type varchar to numeric
As you didn't mention this error, the client should pass something other than ''. The problem can be found with SQL Profiler: you need to run it and see what exact SQL statement is executing to insert data into the table.

How to parse colon within string in Oracle

I wish to insert a record to an Oracle database where one field contains the path to a file, i.e 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt'. But when I attempt an INSERT statement I am asked about binding the value \\ProjectCodes\\ProjectZ\\ZCode.txt.
This is because of the colon I think. Is it possible to parse the colon to insert this value? The target field in the table is declared as a varchar2.
Here is the INSERT statement. I am using SQL Developer. Had to hide the IP address and password etc or I would be in trouble
INSERT INTO SFTP_Creds (SFTP_HOST, SFTP_PORT, SFTP_USERNAME, SFTP_PASSWORD,
SFTP_FINGERPRINT, SFTP_HOSTKEY, SFTP_FILE)
VALUES (‘xxxx.xxxx.xxxx.xxxx’, 22, ‘CORP\\username’, ‘password’, NULL,
NULL, 'C:\\ProjectCodes\\ProjectZ\\ZCode.txt');

comparing input parameter with xml value using like in sql

I have an SQL table with a column which stores xml like this
<AdditionalInfo><RegistrantID>16279</RegistrantID></AdditionalInfo>
I have created a stored procedure like this:
CREATE PROC hr_GetJobStatusByRegistrantId
#registrantId VARCHAR
AS
BEGIN
SELECT TOP 1
[IsSubscribed]
FROM [Hrge].[dbo].[hr_Jobs]
where AdditionalInfo LIKE '%<AdditionalInfo><RegistrantID>%' + #registrantId + '%</RegistrantID></AdditionalInfo>%'
END
When I run this stored procedure, I get null:
exec hr_GetJobStatusByRegistrantId '16279'
If I make this parameter integer then I get convertion to int error.
Please suggest me solution to this.
(Just expanding the comment into an answer)
You should always specify the width of a char or a varchar field, because unless you do the default kicks in. The documentation says:
When n is not specified in a data definition or variable declaration
statement, the default length is 1. When n is not specified when using
the CAST and CONVERT functions, the default length is 30.
which means that in your case you have actually defined #registrantId as VARCHAR(1) so the value of '16279' was trimmed to a single character ('1') and you actually searched for
%<AdditionalInfo><RegistrantID>%1%</RegistrantID></AdditionalInfo>%
in the database. This actually returned the IsSubscribed flag for the first record it found in the DB that had a '1' anywhere in the RegistrantID field. You got lucky that the value was something wrong, so you noticed it.
Additionally you are using % around your parameter. This means that when you search for a RegistrantID of 123, you'll get results for 123, 1234, 2123, 51236, etc, etc, and then just take the first one, whichever that one is (decided by the database, since there is no order clause). It's my guess that you need an exact match, so you should remove those, and just use
'%<AdditionalInfo><RegistrantID>' + #registrantId
+ '</RegistrantID></AdditionalInfo>%'
Also, it the RegistrantId is actually a number, it would be nice if the interface of the procedure reflected that, so it could be defined with
#registrantId int
and then converted to a string in the query
'%<AdditionalInfo><RegistrantID>' + cast(#registrantId as varchar(10))
+ '</RegistrantID></AdditionalInfo>%'

Add column in sql server 2005

I am using SQL Server 2005 and JSP. I want to add column having name as string object value.
String dr="Doctor1";
stat1=conn.createStatement();
stat1.executeUpdate("ALTER TABLE dbo.PHPL ADD '"+dr+"' NVARCHAR(255) Null");
It is giving error near column name.I think I am mistaking with syntax. Please help.
String dr="Doctor1";
stat1=conn.createStatement();
stat1.executeUpdate("ALTER TABLE dbo.PHPL ADD "+dr+" NVARCHAR(255) Null");
that works fine.
A couple of issues here
You need to use execute, not executeUpdate. Altering a table is a DDL command, not insert, update or delete
As KittenLS has pointed out, the syntax for ALTER TABLE doesn't need quotes. However, you should use '[]' around to escape spaces, keywords, etc.
i.e.
stat1.execute("ALTER TABLE dbo.PHPL ADD [" + dr + "] NVARCHAR(255) Null");