Using Variable in the query - sqlite

I'm struggling to make these two lines work without error.
campaignName = result['campaign_name']
db_conn.execute('INSERT INTO Report (CAMPAIGN) VALUES ("' + campaignName + '")')
I thought that the "'" in "result['campaign_name']" was the problem but even if I assigned the variable the error is persistent.
The error:
Thank you,
EDIT: I've modified my request to:
db_conn.execute("INSERT INTO Report (CAMPAIGN) VALUES ('" + str(campaignName.encode()) + "')")
And the script stops at the first dash "-".

I solved the problem this way:
db_conn.execute("INSERT INTO Report (CAMPAIGN) VALUES (?)", (campaignName,))

Related

Code won't execute after recordset returns using if-statement

Can anyone help me explain the following problem
In a SQL-query I get info about the occurrence of an id from another table.
Everything works here and I show a crucial snippet below
set rsRecordCount = Cn.execute(mysql_query)
dim cnt
cnt = rsRecordCount("TotalRecords")
response.write("cnt " & cnt & " id = " & id & "<br>")
rsRecordCount.Close
set rsRecordCount = nothing
So - via response.write I get info about the occurrence. The problem arises
when I want to test the cnt variable
if cnt = 0 then
response.write(".....<br>")
end if
just a simple test, the code stops execute here and I wonder why? I first thought cnt might work like a pointer pointing to a recordset and when setting it to null it would be error or undefined behaviour? But I've tried to comment out .Close and = nothing. The problem is still there.
Its really the cnt that is the prolem, If I use antoher variable - the code works again
if blablabla = 0 then
response.write("it works now<br>")
end if
How do I get around this/what did I miss
thanks!!!
I first thought cnt might work like a pointer pointing to a recordset
You were right in this assumption, but not 100% accurate. The default property of Recordset object returns a Field object, i.e. after this line:
cnt = rsRecordCount("TotalRecords")
The variable cnt is actually a Field object. Now you ask, why this line "works"?
response.write("cnt " & cnt & " id = " & id & "<br>")
That's because the Field object returns its Value property when being treated as a String.
However, when trying to compare to integer, VBScript fails to find any proper conversion and chokes.
There are two common ways to solve such a thing:
Convert the value to integer using CInt or CLng:
cnt = CLng(rsRecordCount("TotalRecords"))
Like the Response.Write above, this one first converts to String, then to Long. Profit.
Take the actual value, not the Field object:
cnt = rsRecordCount.Fields("TotalRecords").Value
Personally, I think the second way is more elegant and readable. Both ways work. Good luck!

Arithmetic overflow error converting expression to data type datetime. in asp.net

This is my code
sqlquery = "select * from Mytables where cname='" + name + "' and cast(cast('" + txtselectdate.Text + "' as char(8)) as datetime)";
Getting error as Arithmetic overflow error converting expression to data type datetime.
Please suggest some ideas
Use tihs
CONVERT(Datetime,txtselectdate.Text,110)
You can use any format instead of 110
or
you can simply pass the converted date in your query like this
DateTime.Parse(txtselectdate.Text)
then you do not need to cast it in query.
===========Update ========
select * from mytable where cname='" + name + "' and yourcolumnname=CONVERT(Datetime,'" + txtselectdate.Text + "',110)"
Add column name in your query by which this textbox value will match.
Hai all thanks for the help I got the answer it was a datatype problem in my database
I havent saved the data type as datetime so they occurs a clash in updating previously it was nvarchar

Npgsql count rows error

The code I have a problem with:
NpgsqlCommand if_ex = new NpgsqlCommand("SELECT count(id_unit) FROM unit WHERE name=" + "'" + tmp + "'", conn);
int ex = (int)if_ex.ExecuteScalar();
Throws an exception:
Specified cast is not valid.
I am trying to get the row count of columns that have the same name (the string that I pass)
i know I should use parameters, but at this point I am only testing a few things so I figured might as well just do it like this for now.
This problem happens because the return type of a query like: select count(*) is an long and not an int. If you change your code to long ex = (long)if_ex.ExecuteScalar(); you will get what you want. I hope it helps.

how to use scope_identity in sqlite

I have issue when i use scop_identity in sqlite it showing the error like near "select": syntax error...i write query like this
string txtSQLQuery = "insert into SerpTrak_Site (SiteName) values ('" + txturl.Text + "')select scope_identity();";
any wrong in this query please help me...
SQLite does not have a function by the name scope_identity
You are probably looking for 'SELECT last_insert_rowid()'
See also this question: Does SQLite support SCOPE_IDENTITY?
Missing a semicolon between the two queries.
string txtSQLQuery = "insert into SerpTrak_Site (SiteName)
values ('" + txturl.Text + "'); select scope_identity();";

ASP.NET query substring

I've got this field in my database year_start_1 and it is an integer field and an example of an ouput is 20100827 I'm trying to create a substring to create year, week, day and change the format to be 27/08/2010
Here's what i'm trying
Dim query as String = "Select * from openquery (devbook, 'SELECT cast(year_start_1 as varchar(8)) as year_start_1, DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2) + DATENAME(MONTH, substring(CAST(year_start_1 AS VARCHAR(8)),4,2) + DATENAME(YEAR, substring(CAST(year_start_1 AS VARCHAR(8)),1,4))) FROM web_statements')"
It's just throwing up an error and I not sure why:
Server was unable to process request
I have tried using convert but it doesn't work.
Any ideas?
UPDATE
with Chris's suggestion
Dim query as String = "Select * from openquery (devbook, 'SELECT year_start_1, cast(year_start_1 as varchar(8)) as year_start_1, substring(CAST(year_start_1 AS VARCHAR(8)),7,2)+''/''+substring(CAST(year_start_1 AS VARCHAR(8)),5,2)+''/''+substring(CAST(year_start_1 AS VARCHAR(8)),1,4) FROM web_statements')"
Still getting the error
Thanks
UPDATE
Couldn't seem to get it to work within the query so had to do a work around in the ASP.Net code
'POINTS END DATE YEAR
Dim strPointsDateEndYear = Mid(drv.Row("year_end_1"), 3, 2)
Dim strPointsDateEndMonth = Mid(drv.Row("year_end_1"), 5,2)
Dim strPointsDateEndDay = Right(drv.Row("year_end_1"), 2)
Dim strPointsDateEnd As String = strPointsDateEndDay + "/" + strPointsDateEndMonth + "/" + strPointsDateEndYear
Thanks for the help though
Your first DATENAME doesn't seem to close all its brackets before the next DATENAME starts:
DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2) + DATENAME[...]
Should I assume be:
DATENAME(DAY, substring(CAST(year_start_1 AS VARCHAR(8)),6,2)) + DATENAME[...]
Edit: Though having fixed that minor error (and converted it to debug) I'm getting errors about casting strings to dates. I'm not sure what the datename stuff is meant to do but how about this:
DECLARE #year_start_1 int
SET #year_start_1 = 20100827
SELECT cast(#year_start_1 as varchar(8)) as year_start_1,
substring(CAST(#year_start_1 AS VARCHAR(8)),7,2)+'/'+substring(CAST(#year_start_1 AS VARCHAR(8)),5,2)+'/'+substring(CAST(#year_start_1 AS VARCHAR(8)),1,4)
(converted to a non table based select for test/debug purposes)
So what you would want for your final line of sql would be (untested):
Select * from openquery (devbook, 'SELECT cast(year_start_1 as varchar(8)) as year_start_1, substring(CAST(year_start_1 AS VARCHAR(8)),7,2)+'/'+substring(CAST(year_start_1 AS VARCHAR(8)),5,2)+'/'+substring(CAST(year_start_1 AS VARCHAR(8)),1,4) FROM web_statements')
Second Edit for debug notes:
I thought it might also be worth suggesting how to debug this sort of problem. The error message suggested that the linked server you were sending the sub-statement to was unable to process the request. The first thing to try in this case would be to run the request directly on the server to see what happens. In this case in fact just parsing it on its own would have revealed the first errors I got.
Once you have your statement running form management studio or whatever directly on the server you can try converting it back into the "openquery" style statement and see if ti still works. Bascially break down your complicated scenario into lots of smaller bits to test each one individually.
select convert(varchar(10),convert(smalldatetime,'20100827'),103) will return 08/27/2010
so, your solution is:
select convert(varchar(10),convert(smalldatetime,convert(varchar,year_start_1)),103) will return 27/08/2010
You are using the datename function on strings, but it should be used on a datetime value. They are nested inside each other so you would end up with just the day, and besided it doesn't even do what you are looking for.
To get the format that you asked for, you can just use string operations to split it up in it's components and add slashes between them:
substring(CAST(year_start_1 AS VARCHAR(8)),7,2) + '/' +
substring(CAST(year_start_1 AS VARCHAR(8)),5,2) + '/' +
substring(CAST(year_start_1 AS VARCHAR(8)),1,4)

Resources