I'm looking for an answer to the following question:
Is it possible to 'Create a Table' and to 'Input Values' with various Arrays?
My Problem is, that I have a large amount of values which I'd like to insert into the table. Because of that it's not really practical for me, to do the standard 'CreateTable' stuff like c.execute('''CREATE TABLE calc_matrix (date text, trans text, symbol text, qty real, price real)''')
I have the following Arrays which I like to insert:
1. Array: column_name [200] = [ColName1 string, ColName2 string, ...., ColName200 string]
2. Array: Result_1 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]
3. Array: Result_2 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]
4. Array: Result_3 [200] = [ Value1 int, Value2 double, Date date, Value3 float, ...., Value185 int, Value 186 float]
Finale Table Example:
[0]|ColName1|ColName2|ColName3|ColName4|...|ColName199|ColName200|
______________________________________________________________
[1]|Value1|Value2|Date|Value3|....|Value198|Value199|
[2]|Value1|Value2|Date|Value3|....|Value198|Value199|
[3]|Value1|Value2|Date|Value3|....|Value198|Value199|
[4]|Value1|Value2|Date|Value3|....|Value198|Value199|
.
.
.
[150]|Value1|Value2|Date|Value3|....|Value198|Value199|
Therefore I don't know how to accomplish that.
I've tried things like:
c.execute('''CREATE TABLE calc_matrix %s''', column_name)
Or in my loop:
c.execute('''CREATE TABLE calc_matrix ?''', column_name[i])
but nothing worked ......
Try to create the full SQL command in a string first and then pass the string into c.execute
string = "CREATE TABLE calc_matrix ( "
your for loop
string += column_name[i] + ", "
string += ")"
c.execute(string)
Your for loop wasn't working before because your program was trying to execute many CREATE TABLE commands that all had the same table name calc_matrix. You need the entire command in one c.execute statement.
The same pattern can be used for inserting many rows into a table.
string = "INSERT INTO calc_matrix VALUES ("
your for loop
string += result_1[i] + ", "
string += ")"
c.execute(string)
Related
i need the values of a json_array. I tried this:
DECLARE
l_stuff json_array_t;
BEGIN
l_stuff := json_array_t ('["Stirfry", "Yogurt", "Apple"] ');
FOR indx IN 0 .. l_stuff.get_size - 1
LOOP
INSERT INTO t_taböe (name, type)
VALUES(l_stuff.get(i), 'TEXT');
END LOOP;
END;
You are passing the position as i instead of indx; but you need a string so use get_string(indx) as #Sayan said.
But if you try to use that directly in an insert you'll get "ORA-40573: Invalid use of PL/SQL JSON object type" because of a still-outstanding (as far as I know) bug.
To work around that you can assign the string to a variable first:
l_name := l_stuff.get_string(indx);
INSERT INTO t_taböe (name, type)
VALUES(l_name, 'TEXT');
db<>fiddle
You do not need PL/SQL and can do it in a single SQL statement:
INSERT INTO t_taböe (name, type)
SELECT value,
'TEXT'
FROM JSON_TABLE(
'["Stirfry","Yogurt","Apple"]',
'$[*]'
COLUMNS (
value VARCHAR2(50) PATH '$'
)
);
db<>fiddle here
First convert the JSON array into an ordinary PL/SQL array, then use a bulk insert.
Here is a reproducible example:
create table tab (name varchar2 (8), type varchar2 (8))
/
declare
type namelist is table of varchar2(8) index by pls_integer;
names namelist;
arr json_array_t := json_array_t ('["Stirfry", "Yogurt", "Apple"]');
begin
for idx in 1..arr.get_size loop
names(idx) := arr.get_string(idx-1);
end loop;
forall idx in indices of names
insert into tab (name, type) values (names(idx), 'TEXT');
end;
/
The query and outcomes:
select * from tab
/
NAME TYPE
-------- --------
Stirfry TEXT
Yogurt TEXT
Apple TEXT
Just use get_string:
DECLARE
l_stuff json_array_t;
BEGIN
l_stuff := json_array_t ('["Stirfry", "Yogurt", "Apple"] ');
FOR indx IN 0 .. l_stuff.get_size - 1
LOOP
--INSERT INTO t_taböe (name, type)
-- VALUES(l_stuff.get_string(indx), 'TEXT');
dbms_output.put_line(l_stuff.get_string(indx));
END LOOP;
END;
Create table statement
create table "DEMO_DB"."PUBLIC"."Trips"
(tripduration integer,
starttime timestamp,
stoptime timestamp,
start_station_id integer,
start_station_name string,
start_station_latitude float,
start_station_longitude float,
end_station_id integer,
end_station_name string,
end_station_latitude float,
end_station_longitude float,
bikeid integer,
usertype string,
birth_year integer,
gender integer);
PUT command
avinash#COMPUTE_WH#DEMO_DB.PUBLIC>put file://C:\Users\lanreddy\Desktop\JC-202002-citibike-tripdata1.csv #%Trips;
**002003 (02000): SQL compilation error: Stage 'DEMO_DB.PUBLIC."%TRIPS"' does not exist or not authorized.**
When you created the table you used double-quotes around the table name which means it is case sensitive: create table "DEMO_DB"."PUBLIC"."Trips". When you do this it means that every time you reference the table now you need to put it in quotes (the table name is Trips and not TRIPS or trips)
Try running this as your PUT command:
COMPUTE_WH#DEMO_DB.PUBLIC> put file://C:\Users\lanreddy\Desktop\JC-202002-citibike-tripdata1.csv #%"Trips";
or this:
COMPUTE_WH#DEMO_DB.PUBLIC> put file://C:\Users\lanreddy\Desktop\JC-202002-citibike-tripdata1.csv #"%Trips";
I would recommend never creating your tables with double-quotes. if you change your original CREATE TABLE statement to the following then your original PUT command will probably work:
create table DEMO_DB.PUBLIC.trips
(tripduration integer,
starttime timestamp,
stoptime timestamp,
start_station_id integer,
start_station_name string,
start_station_latitude float,
start_station_longitude float,
end_station_id integer,
end_station_name string,
end_station_latitude float,
end_station_longitude float,
bikeid integer,
usertype string,
birth_year integer,
gender integer);
i want match string in json string that like:
"ids":[44,53,1,3,12,45]
i want run query in sqlite send only one digit as id and match one of the above id in sql statement
i write this regex "ids":[\[] for matching start of key
but i don't have any idea to match middle id and escape starting id
example:
i have calc_method table like this:
CREATE TABLE "calc_method" (
"calc_method_id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"calc_method_name" TEXT NOT NULL,
"calc_method_value" TEXT NOT NULL
);
in calc_method_value column i store calcMethod class which convert to json using Gson
class calcMethod{
var memberCafeIds:ArrayList<Long>,
var memberBarIds:ArrayList<Long>
}
after i convert calcMethod to json i have output like below and this value store in calc_method_value column:
{"memberCafeIds":[1,2,14,5,44],"memberBarIds":[23,1,5,78]}
now i want select row that match to my regex pattern like if calc_method_value column have memberBarIds with id 1
SELECT * FROM calc_method WHERE calc_method_value REGEXP '"memberCafeIds":\[[:paramId]'
:paramId is method parameter
Regards, a programmer struggle with regex
In Sqlite, use JSON1 functions to work with JSON, not regular expressions. In particular, json_each() to turn the JSON array into a table you can query:
sqlite> CREATE TABLE ex(json);
sqlite> INSERT INTO ex VALUES ('{"ids":[44,53,1,3,12,45]}');
sqlite> SELECT * FROM ex WHERE 1 IN (SELECT value FROM json_each(ex.json, '$.ids'));
json
-------------------------
{"ids":[44,53,1,3,12,45]}
sqlite> SELECT * FROM ex WHERE 50 IN (SELECT value FROM json_each(ex.json, '$.ids'));
sqlite>
So here is my code block
val cols = df.columns
val w = cols(0)
val query1 = s"select $cols(0), square(age) as age, age as age2, first_name, last_name from test"
val query2 = s"select $w, square(age) as age, age as age2, first_name, last_name from test"
Query 2 works just fine, query 1 throws the following error
no viable alternative at input 'select ['(line 1, pos 7)
== SQL ==
select [Ljava.lang.String;#7988d54(0), square(age) as age, age as age2, first_name, last_name from test
Is there anyway to accomplish query1 without creating a temp variable? I want to acces the indexed cols datatype directly.
You can do it by nesting an expression within the string you're interpolating:
s"select ${col(0)} and then..."
s"select ${df.columns(0)} and then..."
You use the ${expr} to hold some expr which is valid code. The single variable version of the interpolation phrase $foo is actually short hand for ${foo}.
I am using JDBC to create a temporary table, add records to it (with prepared statement and batch) and then transfer everything to another table:
String createTemporaryTable = "declare global temporary table temp_table (RECORD smallint,RANDOM_INTEGER integer,RANDOM_FLOAT float,RANDOM_STRING varchar(600)) ON COMMIT PRESERVE ROWS in TEMP";
statement.execute(createTemporaryTable);
String sql = "INSERT INTO session.temp_table (RECORD,RANDOM_INTEGER,RANDOM_FLOAT,RANDOM_STRING) VALUES (?,?,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
float f = 0.7401298f;
Integer integer = 123456789;
String string = "This is a string that will be inserted into the table over and over again.";
// add however many random records you want to the temporary table
int numberOfRecordsToInsert = 35000;
for (int i = 0; i < numberOfRecordsToInsert; i++) {
preparedStatement.setInt(1, i);
preparedStatement.setInt(2, integer);
preparedStatement.setFloat(3, (float) f);
preparedStatement.setString(4, string);
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
// transfer everything from the temporary table just created to the main table
String transferFromTempTableToMain = "insert into main_table select * from session.temp_table";
statement.execute(transferFromTempTableToMain);
This works fine up to about 30000 records in this example. However, if I were to insert say 35000 records I get the following error:
Invalid data conversion: Requested conversion would result in a loss
of precision of 32768. ERRORCODE=-4461, SQLSTATE=42815
The problem is that field RECORD is a smallint. A smallint is a signed 16 bit integer with a range of -32768 to 32767.
So inserting an int value of 32768 is not allowed as it won't fit. You need to declare record as INTEGER instead.