I'm using SQLite & in that i'm using the following query-
INSERT INTO Contact(FirstName, LastName, MobileNumber, IsArchive) VALUES('mina', 'Ambani', '9874587458', 1); SELECT last_insert_rowid() AS 'Identity';
this query insert the record & also gives last insert row id of identity column in my table but initially it generates error as-
The following errors were encountered while parsing the contents of the SQL pane:
Unable to parse query text
How to remove this error?
thanks.
Sqlite does not seem to support multiple SQL statements in one query. Run a separate subsequent query. Something like
SQLiteCommand IDCmd = new SQLiteCommand("Select last_insert_rowid();", conn);
cmd.ExecuteNonQuery(); // EXECUTE INSERT HERE
long id = (long)IDCmd.ExecuteScalar();
Connection open/close and error handling is missing from this example.
Related
I want to insert a new row into a database using R and the odbc package.
The setup is as follow:
I use SQL Server to host the database
I have setup an ODBC Driver on my machine in order to connect to the database
I created a new table
CREATE TABLE [dbName].[dbSchema].[TestTableName]
(
MyID INT IDENTITY(1,1),
MyValue VARCHAR(255),
PRIMARY KEY (MyID)
)
As far as I understand the problem it doesn't matter how the table was created.
Now I want to insert a new entry to this table and keep the new auto incremented value of MyID. To this end I can run the following SQL statement:
INSERT INTO [dbName].[dbSchema].[TestTableName] (MyValue)
VALUES ('test');
SELECT SCOPE_IDENTITY() as newID;
When I run this statement in SQL Server Management Studio, it happily returns a 1x1 table containing the new ID.
And now my problem starts: when I try to do the same form within R by using:
con <- odbc::dbConnect(
odbc::odbc(),
dsn = ...,
database = "dbName",
UID = ...,
PWD = ...
) # the connection is tested and works
createQuery <- "INSERT INTO [dbName].[dbSchema].[TestTableName] (MyValue ) VALUES ('test'); SELECT SCOPE_IDENTITY() as newID;"
dbSendRes <- odbc::dbSendQuery(conn = con, statement = createQuery)
The result in dbSendRes is:
<OdbcResult>
SQL INSERT INTO [dbName].[dbSchema].[TestTableName] (MyValue ) VALUES ('test'); SELECT SCOPE_IDENTITY() as newID;;
ROWS Fetched: 0 [complete]
Changed: 1
Hence, the insert statement is performed successfully on the server, however I do not get the newly assigned ID as a result. The content of dbFetch is an empty data.frame. I also tried using dbSendStatement and dbExecute without any progress. I also tried digging into the additional parameters like immediate=TRUE without any success. I also tried to switch back to the old RODBC package and use the methods therein. Nothing works.
Am I missing something?
Problem remains that I somehow have to keep track on the new ID which is assigned to the db entry! Without that ID I can't proceed. I could do a workaround and query the max ID after complete insert by another separate statement, however this may of course lead to other more fundamental problems if in the meantime another statement would insert another entry...
Happy for any suggestions!
Best
Based on zero knowledge of R... To have the INSERT return the newly created id you could add an OUTPUT clause to the INSERT statement. It appears this would work.
Something like this
INSERT INTO [dbName].[dbSchema].[TestTableName] (MyValue )
output inserted.MyID
VALUES
('test');
I'm trying to read BLOB data from messages table form WhatsApp database called msgstore, I want to get the file names that are transferred. The blob data is stored in thumb_image column.
I found this query here:
SELECT dbms_lob.substr(thumb_image)
FROM messages
WHERE _id = 3;
I also tried this query from here:
SELECT utl_raw.cast_to_varchar2(dbms_lob.substr(thumb_image))
FROM messages
WHERE _id = '3;
I both cases I keep getting an error thats says:
Error while executing sql query on database 'msgstore': near "(" syntax error.
I don't understand what's wrong with the query.
How can I fix this?
I am trying to insert clients on a table(importing them from a csv), the email column needs to be unique(in the csv the a client can appear more than one time, the last instance has the correct info) and the ids are created with an autoincrementing value (this is why i cant use select or replace), im trying to use the syntax found in the sqlite guide(and in every question about ON CONFLICT DO UPDATE found here) but sqlite throws a
SQL logic error near "ON" :syntax error
the query goes like this(using VB)
sqlQuery = "INSERT INTO clients(name1,name2,address1,address2,plz,city,country,phoneNumber1,phoneNumber2,cellPhoneNumber,fax,email) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) ON CONFLICT(email) DO UPDATE SET name1=excluded.name1,name2=excluded.name2,address1=excluded.address1,address2=excluded.address2,plz=excluded.plz,city=excluded.city,country=excluded.country,phoneNumber1=excluded.phoneNumber1,phoneNumber2=excluded.phoneNumber2,cellPhoneNumber=excluded.cellPhoneNumber,fax=excluded.fax,email=excluded.email;
I am trying to write a paramaterized update query to insert values into an Sql Server Express Database. The query I have written is:
Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandText = "update tblposts set title=#ptitle, pdate=#pd,
content=#pcontent where pid=#p"
cmd.Parameters.AddWithValue("ptitle", txtTitle.Text)
cmd.Parameters.AddWithValue("pcontent", txtcontent.InnerText)
cmd.Parameters.AddWithValue("pd", DateTime.Now.ToString)
cmd.Parameters.AddWithValue("p", postid)
On running cmd.ExecuteNonQuery, I get number of rows affected as 1, but the change is not reflected in the database.
On printing the query using Debug.Write, I get the query not with the parameter values, but the names of the parameters itself (ie. #pcontent, #title etc)
What can be the mistake here?
In you're AddWithValue you need to include the # symbol on the front of the parameter, so:
cmd.Parameters.AddWithValue("#ptitle", txtTitle.Text)
cmd.Parameters.AddWithValue("#pcontent", txtcontent.InnerText)
cmd.Parameters.AddWithValue("#pd", DateTime.Now.ToString)
cmd.Parameters.AddWithValue("#p", postid)
I'm guessing that it's executing correctly but there where clause is blank, so perhaps updating a blank row.
Anyway, try the above and it should update as expected.
Edit to Add
The CommandText will always only have the #value in there, it will not substitue the parameter values into the string.
You would need to loop through the cmd.Parameters collection to write out the values.
How to return auto generated column values from sqlite database in c#. I'm using Sqlite.data.dll.
Tried by executing query : "select last_insert_rowid() but it doesn;t seem to work as it is returning 0s. I'm using SqliteDataReader object to get the result.
Thanks
I tried these commands, and it worked fine (even if the last_insert_rowid() call is from another transaction):
sqlite> create table mytable(pk integer primary key, other stuff);
sqlite> insert into mytable values(null, 42);
sqlite> select last_insert_rowid();
1
The only requirement of last_insert_rowid() is that it is called on the same database connection; please check that you didn't open another one.
is your id is incremented? then try this:
SELECT max(id)
or you can refer to this post
Click HERE