I am working integrating SQFLite plugin in a flutter. For the first time table is created and everything is working but when I try to open the database for the second time for running query it gives me a sqflite error.
<code>
Future<Database> initializeDatabase() async {
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
String path = join(documentsDirectory.path, databaseName);
Database database = await openDatabase(
path,
version: 1,
onCreate: (Database db, int newVersion) async {
await db.execute(
'''
CREATE TABLE $tableName(ss INTEGER , dd TEXT, uu TEXT)''',
);
print("Created tables");
},
);
return database;
}
insert() async {
Database db = await database;
var id = await db.insert(
tableName,
{
'ss': 1,
'dd': "sdsds",
'uu': "vvvvvvv",
},
);
return id;
// await db.close();
}
</code>
For the first insert query everything works fine but the second time it gives following errors:-
Error:-
Error calling sqlite3_step (1: cannot rollback - no transaction is active) SQLITE_ERROR
DB Query: ROLLBACK
Unknown error finalizing or resetting statement (1: cannot rollback - no transaction is active)
DB Query: ROLLBACK
Related
Why is the #BeforeRemove trigger only executed with the manager.remove() method and it permanently deletes the record from the database table?
Why is this #BeforeRemove trigger not executed by manager.softRemove() for example?
It makes no sense to fill a deleted_at column and then delete the record using manage.remove()!
#BeforeRemove()
delete() {
this.deleted_by = 'MyName'
}
// Run only with manage.remove
async remove(request: Request, _response: Response, next: NextFunction) {
let userToRemove: any = await this.userRepository.findOne(request.params.id);
return await this.userRepository.softRemove(userToRemove);
}
// softRemove does not run #BeforeRemove /:
https://github.com/typeorm/typeorm/releases/tag/0.2.42
Resolved in the release 0.2.42
https://github.com/typeorm/typeorm/pull/8403
I am trying to execute a snowflake stored procedure via python glue job, but when I get any error in the SQL statements in the stored procedure, I want the procedure to exit and that glue job should fail.
I am using this code in my snowflake stored procedure:
create or replace procedure schema.PROC_TEST
(input_sql varchar(16777216))
returns string not null
language javascript
as
$$
snowflake.execute({sqlText:`BEGIN TRANSACTION;`});
try
{
var QueryExec = INPUT_SQL+ ";";
var QueryResult = snowflake.execute({sqlText: QueryExec});
}
catch (err) // Catching any error if occurs in INPUT_SQL
{
result = 'Error: ' + err;
snowflake.execute({sqlText:`ROLLBACK;`});
return result;
throw err.message;
}
$$;
Now if I pass input query as below (here proc_test_final1 is an incorrect table name) :
insert into schema.proc_test_final1
select id, name, sal
from schema.proc_test_src;
Instead of throwing an error 'incorrect table name', the procedure returns a generic error like
Error: 090232 (25000): Stored procedure execution error: Scoped transaction started in stored procedure is incomplete and it was rolled back
I thought it might be because of ROLLBACK command so I tried removing the command but still it failed with the same above error. I also tried removing result variable in catch block but still above error was caught.
I want to catch the exact specific SQL error (syntax error/code error) in my glue job.
Below is my glue job code where I am catching the error:
input_sql = 'insert into schema.proc_test_final1 select id, name, sal from schema.proc_test_src'
try:
f_CallProc = "call TEST_PROC('"+input_sql+"');"
cur.execute(f_CallProc)
print(cur.fetchall())
cur.close()
ctx.close()
except snowflake.connector.errors.ProgrammingError as err:
print('Error: {0} ({1})'.format(err.msg, err.sfqid))
cur.close()
ctx.close()
sys.exit(1)
The error "Scoped transaction started in stored procedure is incomplete and it was rolled back" will happen when you run the SP that has explicit START TRANSACTION, but no explicit "ROLLBACK" or "COMMIT".
In my test, if I do not have below line:
snowflake.execute({sqlText:`ROLLBACK;`});
Your SP will fail with such error.
However, when I have this line, it will fail with below error instead:
Error: SQL compilation error:
Schema 'ERICLIN.SCHEMA' does not exist or not authorized.
Which I think is what you are after.
Can you please double check your SP again? Did you copy and paste the correct one?
By the way, your original SP won't run because there is no variable called "input_sql", it should be "INPUT_SQL", as every variable in Snowflake's SP is in UPPER case.
=========================
UPDATE
Please see below execution output:
create or replace procedure PROC_TEST
(input_sql varchar(16777216))
returns string not null
language javascript
as
$$
snowflake.execute({sqlText:`BEGIN TRANSACTION;`});
try {
var QueryExec = INPUT_SQL + ";";
var QueryResult = snowflake.execute({sqlText: QueryExec});
}
catch (err) {
result = 'Error: ' + err;
snowflake.execute({sqlText:`ROLLBACK;`});
return result;
}
$$;
call PROC_TEST('insert into schema.proc_test_final1 select id, name, sal from schema.proc_test_src');
+-----------------------------------------------------------+
| PROC_TEST |
|-----------------------------------------------------------|
| Error: SQL compilation error: |
| Schema 'ERICLIN.SCHEMA' does not exist or not authorized. |
+-----------------------------------------------------------+
Without the ROLLBACK call, it will produce an error same as yours:
create or replace procedure PROC_TEST
(input_sql varchar(16777216))
returns string not null
language javascript
as
$$
snowflake.execute({sqlText:`BEGIN TRANSACTION;`});
try {
var QueryExec = INPUT_SQL + ";";
var QueryResult = snowflake.execute({sqlText: QueryExec});
}
catch (err) {
result = 'Error: ' + err;
return result;
}
$$;
call PROC_TEST('insert into schema.proc_test_final1 select id, name, sal from schema.proc_test_src');
090232 (25000): Stored procedure execution error: Scoped transaction started in stored procedure is incomplete and it was rolled back.
I had a stored procedure to insert the data from a stage which is called by task. if the load fails(due to on_error= skip_file option) it throws an error which is handled. but the status of the task shows as success. what to do to make the task fail when there is error in stored procedure.
here is the sample code
create or replace procedure sample_procedure()
returns varchar not null
language javascript
execute as caller
as
$$
try
{
try
{
var ct_table_cmd = `create or replace table sample_table_temp like sample_table`;
var ct_table_stmt = snowflake.createStatement({sqlText: ct_table_cmd});
var result_set= ct_table_stmt.execute();
result_set.next();
}
catch(err)
{
var queryId = ct_table_stmt.getQueryId();
var queryText = ct_table_stmt.getSqlText();
var log_insert_into=snowflake.createStatement({sqlText:`insert into error_table
(code, message, queryid, querytext) VALUES (?,?,?,?);`
,binds : [err.code, err.message,queryId,queryText]
});
log_insert_into.execute();
var ct_task_ret_value = snowflake.createStatement({sqlText: `call system$set_return_value('{err.message'});`}).execute();
return err.message;
}
var copy_cmd = `copy into smaple_table_temp from #mystage
file_format=(format_name= 'sample_csv_format')
files=('/file_name')
on_error=skip_file;`;
var copy_cmd_stmt = snowflake.createStatement({sqlText:copy_cmd});
var result_set =copy_cmd_stmt.execute();
result_set.next();
if(result_set.getColumnValue(2)=='LOADED')
{
var swap_cmd = `alter table sample_table_temp swap with sample_table;`;
var swap_stmt = snowflake.createStatement({sqlText: swap_cmd});
swap_stmt.execute();
}
else if (result_set.getColumnValue(2)== 'LOAD_FAILED')
{
var err_message= result_set.getColumnValue(7);
var queryId = copy_cmd_stmt.getQueryId();
var queryText = copy_cmd_stmt.getSqlText();
var log_insert_into=snowflake.createStatement({sqlText:`insert into error_table
(code, message, queryid, querytext) VALUES (?,?,?,?);`
,binds : [err.code, err.message,queryId,queryText]
});
var task_ret_value = snowflake.createStatement({sqlText: `call system$set_return_value('{err_message'});`}).execute();
log_insert_into.execute();
return err_message
}
}
$$
;
The error log table and task to call the procedure is :
create or replace table error_table (ts timestamp_ntz, src varchar(50),code varhcar(100) ,
message varchar, queryid varchar, querytext varhcar);
create or replace task sample task
warehouse = 'my_warehouse'
schedule= '10 minute'
as
call sample_procedure();
when the task call the procedure with no data in the location it throws an error in the task history as "execution error in stored procedure sample procedure:
SQL compilation error:
Syntax error line at position 47 unexpected 'remote'. at statement.execute, line 172 position 111". but the actual error is "remote file is not existed at. there are several potential causes. The file might not exist. The required credentials may be missing or invalid."
and when the table is not existed, the error shown is different in task_history of information schema. It is not showing the error which is shown when the procedure is called independently (i.e call sample_procdure()).
Any suggestions are helpful.
You're setting the return value for the task here:
var ct_task_ret_value = snowflake.createStatement({sqlText: `call system$set_return_value('{err.message'});`}).execute();
You're setting the return for the stored procedure here:
return err.message;
The problem is err.message is a string containing the text of an error message, which is not an error indication. You've caught the error, so as far as Snowflake's concerned it's been handled.
To indicate an error on SP execution, you can either 1) not catch the error or 2) catch the error, handle it, and throw an error.
Option 2 seems to be what you're looking to do. Catch the error as you're already doing, but instead of returning err.message, do this:
//return err.message;
throw err.message;
Snowflake will wrap the error message with its own error text. There is currently no way to avoid that, so you may want to do something in the text to call out attention to your error text like *wrapping in stars* or something like that. Snowflake Stored Procedure Exception & Failure
I have several Snowflake Stored Procedures and when they are successful, it has the custom message that I created displayed, I would rather be displaying the same message that Snowflake shows in the Query Results window.
For Example, I execute a Stored Proc with the COPY TO statements. I would like the Successful execution of this Stored Proc to show the number of Rows successfully Exported. Can they success information be captured and displayed as easily as the Error messages are?
Yes, you can do this using JavaScript stored procedures. When Snowflake runs a query that returns only a status message, it returns it as a table with a single column "status" and a single row with the status. You can return that value. You may want to consider what happens if there's a SQL error: handle it locally in the SP or throw the error so the calling session knows there was an error. Either way the status return will show an error message if there is one.
Here's a sample using JavaScript SP. It also has some helper functions that I use routinely to execute single value queries and non-query statements just looking for a return value:
create or replace procedure SampleSP()
returns string
language javascript
as
$$
try{
return ExecuteNonQuery("create table MY_NATION_TABLE as select * from SNOWFLAKE_SAMPLE_DATA.TPCH_SF1.NATION;");
}
catch(err){
return err;
}
// ----------------------------------------------------------------------------------
// Main function above; helper functions below
function ExecuteNonQuery(queryString) {
return ExecuteSingleValueQuery("status", queryString)
}
function ExecuteSingleValueQuery(columnName, queryString) {
var out;
cmd1 = {sqlText: queryString};
stmt = snowflake.createStatement(cmd1);
var rs;
try{
rs = stmt.execute();
rs.next();
return rs.getColumnValue(columnName);
}
catch(err) {
if (err.message.substring(0, 18) == "ResultSet is empty"){
throw "ERROR: No rows returned in query.";
} else {
throw "ERROR: " + err.message.replace(/\n/g, " ");
}
}
return out;
}
$$;
-- Run this twice to see the effect of an error. You can remove the try block
-- in the main function of the SP to generate a SQL error instead of just
-- returning a string with the error text
call SampleSP();
I am migrating the API to TypeORM from mongoose. I have an array(conversationIds) containing list of id to be searched from the Database and it should return me the available id. Below are the query which i have tried.
const conversationMessagesWithDuplicates = await consversationRepository.createQueryBuilder("conversation").where("conversation.channel_message_ID = :channel_message_ID",{channel_message_ID : conversationIds}).getMany();
and also this one
const conversationMessagesWithDuplicates = await consversationRepository.find({channel_message_ID: conversationIds});
But i am getting below issue:
(node:11548) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): QueryFailedError: Error: Incorrect syntax near ','.
I have tried using entity Manger also but no success
const conversationMessagesWithDuplicates = await entityManager.query(`SELECT channel_message_ID FROM Conversation where channel_message_ID IN (conversationIds)`);
Below is the MoongoseAPI which is working fine:
Conversation.find( { channel_message_ID: { $in: conversationMessageIdArray } } ).exec(function(err, conversationMessagesDuplicatesArray) {}
This is the solution
const conversationMessagesWithDuplicates = await consversationRepository.createQueryBuilder("conversation").where("conversation.channel_message_ID IN (:conversationIds)", { conversationIds: conversationIds }).getMany();