Obtaining inserted sqlite from command line - sqlite

I use bash to access a sqlite3 database.
Is there a way to get the last inserted id that way, because on the sqlite-webpage (http://www.sqlite.org/c3ref/last_insert_rowid.html) I only see the C-API, but no way to do it on the command line.
The function last_insert_rowid() does not work either:
sqlite> last_insert_rowid();
SQL error: near "last_insert_rowid": syntax error

should be
select last_insert_rowid();

Related

The new line doesn't work when generating xml

let's suppose to have the table
create table mytable
(val1 number(5),
val2 varchar2(10));
insert into mytable values (1,'XXX');
and asked to generate the following XML
<ns1:head>
<ns1:val1>1</val1>
<ns1:val2>XXX</val2>
</ns1:head>
It's quite simple by running
select xmlelement("head",
xmlelement("val1",val1).extract('/*'),
xmlelement("val2",val2).extract('/*')
).extract('/*')
from mytable;
and to get
<head>
<val1>1</val1>
<val2>XXX</val2>
</head>
The problem is that if I try to do that way but for getting each node with "ns1:" in front of every tag
select xmlelement("ns1:head",
xmlelement("ns1:val1",val1).extract('/*'),
xmlelement("ns1:val2",val2).extract('/*')
).extract('/*')
from mytable;
I get an ORA-31011: XML parsing failed
Maybe I dont' konw how exactly the .extract('/*') works and in my case the "ns1:" could fails
Oracle version 10g
Thanks in advance!
Mark
What is the purpose of using extract('/*')?
Also a namespace prefix must be defined. You can add its declaration in the root element with xmlattributes.
select xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1),
xmlelement("ns1:val2",val2)
)
from mytable;
result:
<ns1:head xmlns:ns1="http://www.example.com/ns1"><ns1:val1>1</ns1:val1><ns1:val2>XXX</ns1:val2></ns1:head>
Update:
XMLELEMENT returns and XML object. If you want to get it as a formatted text, you can use XMLSERIALIZE.
select XMLSERIALIZE(document xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1),
xmlelement("ns1:val2",val2)
) indent)
from mytable;
result:
<ns1:head xmlns:ns1="http://www.example.com/ns1">
<ns1:val1>1</ns1:val1>
<ns1:val2>XXX</ns1:val2>
</ns1:head>
Update 2:
as I found out, indent instruction does not exist in Oracle 10g. So, if extract('/*') worked without namespaces, then adding xmlattributes to your original query with namespaces might work. I don't have Oracle 10g to test this.
select xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1).extract('/*'),
xmlelement("ns1:val2",val2).extract('/*')
).extract('/*')
from mytable;
Update 3:
Using extract('/*') for indenting lines looks like an undocumented feature. On Oracle 12c it doesn't work this way. So, updating Oracle version might break this undocumented behavior.

Why is my Airflow MySqlOperator Insert Command Denied?

I'm running a dag with an insert query. Here is some of the code:
QUERY = '''
INSERT INTO bi.target_skus (skus)
SELECT
distinct od.sku,
FROM
bi.orders as od'''
t1 = MySqlOperator(
sql=QUERY,
mysql_conn_id = MYSQL_CONN_ID,
task_id='target_skus',
dag=dag)
It's giving me the following error:
ERROR - (1142, "INSERT command denied to user 'xyz' for table 'target_skus'")
A few notes:
Devops said my user has permission to make inserts into that table
Select commands work fine
The error message does not include the database name (bi) even though my insert query does.
This looks like a standard MySQL "not enough privileges" error.
Are you sure you can perform INSERTs with your user, regardless of what your DBA is saying? You should test the same operation using another tool (like MySQL Workbench) setting up the connection in the same way you set it up in Airflow, i.e. same user, same password, same default schema.
It looks like a privilege error from the user trying to insert but there is a comma in the insert that can cause problems too:
QUERY = '''
INSERT INTO bi.target_skus (skus)
SELECT
distinct od.sku
FROM
bi.orders as od'''

sqlite3 got syntax error while executing vacuum in a trigger

I'm using sqlte3.8.8, trying to create a trigger to clean old data. Here is the SQL that I put in:
CREATE TRIGGER "main"."NewTrigger" AFTER INSERT ON "historydata"
BEGIN
delete from historydata where id in (select id from historydata order by id limit 100000);
vacuum;
END;
But I got Syntax error on "vacuum;".However, it works fine in sqlite command line.
Is it the case that "vacuum" cannot be used in a trigger?
The documentation shows that only UPDATE/INSERT/DELETE/SELECT statements are allowed in a trigger body.

java.sql.SQLSyntaxErrorException: ORA-04098: trigger 'MYTABLE.MY_SEQUENCE_ID' is invalid and failed re-validation

I am creating this oracle 11g statement inside a java String and then executing it in sql developer. I tried running it on the database and got a warning when the trigger
is created. But, when run from the code, I get the error mentioned in my title.
Please tell me where is the mistake and how i can fix it ?
CREATE OR REPLACE TRIGGER myschema.my_sequence_id BEFORE INSERT ON myschema.mytable
FOR EACH ROW BEGIN SELECT my_sequence_id.nextval INTO :new.mycolumn FROM DUAL; end; /
Thanks in advance !
You can't have a trigger named my_sequence_id if you already have a sequence my_sequence_id. They share the same namespace. Your trigger would need to be named something other than the name of the sequence (or any other object in the schema).

No table found error in SQlite

I have a problem with SQlite. Whenever I execute Queries it goes fine but when I exit by .exit command and open again and execute Select * form emp statement, it renders no table even though I use begin transaction and commit .
Any help please. I'm new to SQLite.
Thanks
Bamadeva
I got the solution:-
Wherever our database is created, in my case it is :-
C:\Sites\databases>
Since my database is contactlist_development, I should have entered it with the following commands:-
C:\Sites\databases>sqlite3 contactlist_development
This takes me to :-
sqlite>.schema
Then i am able to view the table details which i just created:)

Resources