What is wrong with this SQLite create Statement in AndroidStudio - sql

Can anyone decipher what's wrong with this method? The log says that there's is a syntax error but I'm not able to see it.
Here's the code:
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String createTable = "CREATE_TABLE " + MOVIES_DB + "("
+ TITLE + " TEXT, "
+ OVERVIEW + " TEXT, "
+ POSTER_PATH + " TEXT, "
+ RELEASE_DATE + " TEXT, "
+ ORIGINAL_TITLE + " TEXT, "
+ ORIGINAL_LANGUAGE + " TEXT, "
+ BACKDROP_PATH + " TEXT, "
+ ID + " INTEGER PRIMARY KEY, "
+ VOTE_COUNT + " TEXT, "
+ POPULARITY + " TEXT, "
+ VOTE_AVERAGE + " TEXT, "
+ ADULT + " TEXT, "
+ VIDEO + " TEXT, "
+ GENRE_IDS + " TEXT, "
+ HISTORY_USER + " TEXT, "
+ WATCH_LATER + " TEXT, "
+ FAVORITE_FLAG + " INTEGER DEFAULT 0"
+ ")";
sqLiteDatabase.execSQL(createTable);
}
It used to work. I added the FAVORITE_FLAG and now it doesn't run. I also tried with INTEGER NOT NULL DEFAULT 0 after FAVORITE_FLAG

As said by #Juergen D, CREATE TABLE take a space and not and underscore.
Take a look at the CREATE TABLE Documentation for SQLite.
BTW; try to put your id / PK declaration first, as it's the common practice (and more logical)
SQL Fiddle
SQLite (SQL.js) Schema Setup:
CREATE TABLE MOVIES_DB(
"ID" INTEGER PRIMARY KEY,
"TITLE" TEXT,
"OVERVIEW" TEXT,
"POSTER_PATH" TEXT,
"RELEASE_DATE" TEXT,
"ORIGINAL_TITLE" TEXT,
"ORIGINAL_LANGUAGE" TEXT,
"BACKDROP_PATH" TEXT,
"VOTE_COUNT" TEXT,
"POPULARITY" TEXT,
"VOTE_AVERAGE" TEXT,
"ADULT" TEXT,
"VIDEO" TEXT,
"GENRE_IDS" TEXT,
"HISTORY_USER" TEXT,
"WATCH_LATER" TEXT,
"FAVORITE_FLAG" INTEGER DEFAULT 0
);
Insert into MOVIES_DB Values(1,"test","test","test 7",
"test","test 12","test","test","test","test","test",
"test","test","test","test","test",2);
Query 1:
select ID, TITLE, FAVORITE_FLAG from MOVIES_DB
Results:
| ID | TITLE | FAVORITE_FLAG |
|----|-------|---------------|
| 1 | test | 2 |

Related

Dynamic dates in where parameter

Am I able to use a dynamic date range for the "where" parameter? I'd like to get all the data changed in the last 30 mins.
All I can find in the documentation is using static dates:
https://developer.xero.com/documentation/api/requests-and-responses
You could try using the query string in the Where Clause.
For Ex.
string querystr = "Date >= " +
"DateTime(" + dateFrom.Year.ToString() + ",
" + dateFrom.Month.ToString() + ", " + dateFrom.Day.ToString() + ") " +
"&& Date <= " +
"DateTime(" + dateTo.Year.ToString() + ", "
+ dateTo.Month.ToString() + ", " + dateTo.Day.ToString() + ")";
Passing this querystring to retrieve user defined date rage Invoices

Hive table load in Parquet format

I have the below input file. I need to load this file in hive table in orc and parquet format.
productID,productCode,name,quantity,price,supplierid
1001,PEN,Pen Red,5000,1.23,501
1002,PEN,Pen Blue,8000,1.25,501
I have pasted my code in the bottom. I am able to successfully create and load in orc hive table but not in parquet.
After creating and loading the parquet table, when i query, i see only NULL values for all fields. Am i missing anything?
val productsupplies = sc.textFile("/user/cloudera/product.csv")
val productfirst = productsupplies.first
val product = productsupplies.filter(f => f != productfirst).map(x => { val a = x.split(",")
(a(0).toInt,a(1),a(2),a(3),a(4).toFloat,a(5))
}).toDF("productID","productCode","name","quantity","price","supplierid")
product.write.orc("/user/cloudera/productsupp.orc")
product.write.parquet("/user/cloudera/productsupp.parquet")
val hc = new org.apache.spark.sql.hive.HiveContext(sc)
hc.sql("create table product_supp_orc ( " +
"product_id int, " +
"product_code string, " +
"product_name string, " +
"product_quatity string, " +
"product_price float, " +
"product_supplier_id string) stored as orc " +
"location \"/user/cloudera/productsupp.orc \" ")
hc.sql("create table product_supp_parquet ( " +
"product_id int, " +
"product_code string, " +
"product_name string, " +
"product_quatity string, " +
"product_price float, " +
"product_supplier_id string) stored as parquet " +
"location \"/user/cloudera/productsupp.parquet\" ")
hc.sql("select * from product_supp_parquet")
Try:
hc.sql("create table product_supp_parquet ( " +
"productid int, " +
"productcode string, " +
"name string, " +
"quantity string, " +
"price float, " +
"supplierid string) stored as parquet " +
"location \"/user/cloudera/products.parquet\" ")
Basically, the names must be the same as what you used in the file for uploading.

SQLite Insert or Replace using angularJS

I'm trying to implement a INSERT OR REPLACE SQL to update the values that come from my API, the problem is, every time that I register a new item in my API and reload the app, it is duplicating all the rows, it is inserting the whole data again, how can I prevent that to happen?
angular.forEach(item.faturamentos, function (fat) {
//debugger
db.transaction(
function (tx) {
tx.executeSql('INSERT OR REPLACE INTO faturamento_pedidos (valor_a_faturar, ' +
'nota_fiscal, ' +
'_criado,' +
'_modificado , ' +
'_status, ' +
'id_rm, ' +
'cod_id, ' +
'id_rm_pedido, ' +
'id_rm_empresa, ' +
'data, ' +
'informacoes_adicionais ) VALUES (?,?,?,?,?,?,?,?,?,?,?)',
[
fat.valor_a_faturar,
fat.nota_fiscal,
fat.criado,
fat.modificado,
fat.status,
fat.id,
fat.cod_id,
fat.id_rm_pedido,
fat.id_rm_empresa,
fat.data,
fat.informacoes_adicionais
]);
},
txErrorHandler,
function () {
log('Record inserted successfully');
}
);
});
TABLE:
tx.executeSql("CREATE TABLE IF NOT EXISTS faturamento_pedidos (" +
"faturamento_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"id_rm_pedido Integer, " +
"id_rm_empresa Integer, " +
"cod_id Text, " +
"valor_a_faturar Text, " +
"nota_fiscal Text, " +
"data Text, " +
"informacoes_adicionais Text," +
"CONSTRAINT unique_id_rm UNIQUE ('id_rm'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "faturamento_pedidos.index_faturamento_id" ON "faturamento_pedidos"("faturamento_id");');

INSERT OR REPLACE messing with ID SQLite

My app works like this, I have a local database that is filled with data that comes from my API, and when I have a new data inserted in my API the app checks for the last modified item and synchronize it, and in order to achieve that I'm using INSERT OR REPLACE statement, but it is messing up with my "faturamento_id", it is deleting the ids and replacing with new ones, I want it to continue auto increment(if it is possible) when there is new data to synchronize. How can I do that?
angular.forEach(item.faturamentos, function (fat) {
db.transaction(
function (tx) {
tx.executeSql('INSERT OR REPLACE INTO faturamento_pedidos (valor_a_faturar, ' +
'nota_fiscal, ' +
'_criado,' +
'_modificado , ' +
'_status, ' +
'id_rm, ' +
'cod_id, ' +
'id_rm_pedido, ' +
'id_rm_empresa, ' +
'data, ' +
'informacoes_adicionais ) VALUES (?,?,?,?,?,?,?,?,?,?,?)',
[
fat.valor_a_faturar,
fat.nota_fiscal,
fat.criado,
fat.modificado,
fat.status,
fat.id,
fat.cod_id,
fat.id_rm_pedido,
fat.id_rm_empresa,
fat.data,
fat.informacoes_adicionais
]);
},
txErrorHandler,
function () {
log('Record inserted successfully');
}
);
});
TABLE:
tx.executeSql("CREATE TABLE IF NOT EXISTS faturamento_pedidos (" +
"faturamento_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"id_rm_pedido Integer, " +
"id_rm_empresa Integer, " +
"cod_id Text, " +
"valor_a_faturar Text, " +
"nota_fiscal Text, " +
"data Text, " +
"informacoes_adicionais Text," +
"CONSTRAINT unique_id_rm UNIQUE ('id_rm'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "faturamento_pedidos.index_faturamento_id" ON "faturamento_pedidos"("faturamento_id");');
INSERT OR REPLACE always removes the old row, if it exists.
However, there is no reason to use a single SQL statement.
Just try to update the old row, and if it was not found, you know you have to insert a new one:
tx.executeSql("UPDATE ...",
[...],
function(tx, result) {
if (result.rowsAffected == 0)
tx.executeSql("INSERT ...", [...]);
});

regularexpression for this text sql statement or a program?

Heres specifically the sql text in a file.
select substr(to_char(ctl.tody_run_dt,'YYYYMMDD'),1,8) tody_yyyymmdd,
substr(to_char(cdr.nxt_proc_dt,'YYYYMMDD'),1,8) nxt_proc_yyyymmdd,
add_months(ctl.tody_run_dt - cdr.past_accru_dys,
- ct.nbr_cycl_for_adj) beg_proc_dt,
from tbl_crd )
and prv.calendar_run_dt =
( select max(calendar_run_dt)
from run_tbl1 prv2
From this i wish to extract all the tables,
This seems pretty complicated to be done through a regexp? Is there a way? Or should i write a program? I just cant come up with an algorithm.
You might be able to do something like a linear search like this. It relaxed for your example
and just targets the from keyword, excludes other keywords.
The table data is captured in group 1. It has to be split appart upon each
match through the find loop.
# from\s+((?!(?:select|from|where|and)\b)\w+(?:[,\s]+(?!(?:select|from|where|and)\b)\w+)*)
from
\s+
( # (1 start), Contains all the table info
(?! # exclude keywords
(?:
select
| from
| where
| and
)
\b
)
\w+
(?:
[,\s]+
(?! # exclude keywords
(?:
select
| from
| where
| and
)
\b
)
\w+
)*
) # (1 end)
Perl test case
$/ = undef;
$str = <DATA>;
while ( $str =~ /from\s+((?!(?:select|from|where|and)\b)\w+(?:[,\s]+(?!(?:select|from|where|and)\b)\w+)*)/g )
{
print "\n'$1'";
}
__DATA__
select substr(to_char(ctl.tody_run_dt,'YYYYMMDD'),1,8) tody_yyyymmdd,
substr(to_char(cdr.nxt_proc_dt,'YYYYMMDD'),1,8) nxt_proc_yyyymmdd,
add_months(ctl.tody_run_dt - cdr.past_accru_dys,
- ct.nbr_cycl_for_adj) beg_proc_dt,
(ctl.tody_run_dt + cdr.futr_accru_dys) end_proc_dt,
ctl.tody_end_proc_dt,
ctl.prv_end_proc_dt,
cdr.fst_proc_dy,
cdr.lst_proc_dy,
cdr.accru_nbr_of_dys,
cdr.dy_of_wk,
from run_tbl1 cdr, runtbl
run_tbl1 prv,
run_tbl_cntl ctl,
tbl_crd ct
where cdr.calendar_run_dt = ctl.tody_run_dt
and ct.nbr_cycl_for_adj =
( select max(nbr_cycl_for_adj)
from tbl_crd )
and prv.calendar_run_dt =
( select max(calendar_run_dt)
from run_tbl1 prv2
where prv2.calendar_run_dt < ctl.tody_run_dt
and prv2.accru_nbr_of_dys = 1 )
and rownum = 1
Output >>
'run_tbl1 cdr, runtbl
run_tbl1 prv,
run_tbl_cntl ctl,
tbl_crd ct'
'tbl_crd'
'run_tbl1 prv2'
First I had to correct some syntax errors within your SQL.
... cdr.dy_of_wk, <<<< the comma is wrong
from run_tbl1 cdr ...
Here the proove of concept using JSQLParser V0.8.9 (https://github.com/JSQLParser/JSqlParser) to extract tablenames.
public static void main(String[] args) throws JSQLParserException {
TablesNamesFinder tfinder = new TablesNamesFinder();
String sql = "select substr(to_char(ctl.tody_run_dt,'YYYYMMDD'),1,8) tody_yyyymmdd, "
+ " substr(to_char(cdr.nxt_proc_dt,'YYYYMMDD'),1,8) nxt_proc_yyyymmdd, "
+ " add_months(ctl.tody_run_dt - cdr.past_accru_dys, "
+ " - ct.nbr_cycl_for_adj) beg_proc_dt, "
+ " (ctl.tody_run_dt + cdr.futr_accru_dys) end_proc_dt, "
+ " ctl.tody_end_proc_dt, "
+ " ctl.prv_end_proc_dt, "
+ " cdr.fst_proc_dy, "
+ " cdr.lst_proc_dy, "
+ " cdr.accru_nbr_of_dys, "
+ " cdr.dy_of_wk "
+ " from run_tbl1 cdr, runtbl, "
+ " run_tbl1 prv, "
+ " run_tbl_cntl ctl, "
+ " tbl_crd ct "
+ " where cdr.calendar_run_dt = ctl.tody_run_dt "
+ " and ct.nbr_cycl_for_adj = "
+ " ( select max(nbr_cycl_for_adj) "
+ " from tbl_crd ) "
+ " and prv.calendar_run_dt = "
+ " ( select max(calendar_run_dt) "
+ " from run_tbl1 prv2 "
+ " where prv2.calendar_run_dt < ctl.tody_run_dt "
+ " and prv2.accru_nbr_of_dys = 1 ) "
+ " and rownum = 1 ";
//parse SQL statement
Select select = (Select) CCJSqlParserUtil.parse(sql);
//extract table names
List<String> tableList = tfinder.getTableList(select);
System.out.println(tableList);
}
and it outputs
[run_tbl1, runtbl, run_tbl_cntl, tbl_crd]