How to insert multiple rows in one execute? SQFLite Flutter - android

I am new using sqflite, I come from android studio where by for's I build a query with more than 1 row, which was this:
INSERT OR REPLACE INTO clientes (codigo, empresa, nombre, e_mail) VALUES (?,?,?,?),(?,?,?,?)
then again using for, I inserted the values with the bind.
My problem is that now I'm trying to do the same function but in Flutter .. and I can't find the way because i didnt find any function called "binds" like in android studio .. I know that with Flutter it is easier to handle the Maps with SQFLite but I don't know the way ... at the moment I have this dynamically armed query:
INSERT OR REPLACE INTO clientes (codigo, empresa, nombre, e_mail) VALUES (?,?,?,?),(?,?,?,?)
How do I now pass the values dynamically in flutter?
because this executes it and inserts it, but of course in the DB what you see is "NULL NULL NULL NULL" clearly because I am not passing the values to the arguments ... could you help me or guide me on how to do it?
I was thinking that if, I could pass those values dynamically from a map? How should I do some practical example could you give me?
or with a list? for ex
[{2, asd, asd, asd#mail.com}, {3, asd, asd, asd#mail.com}]
and dynamically insert each 1 of those 2 items as arguments ... could you give me a practical example if this is possible?
thanks since now

Related

Android: Failing to update row using SQL Extension function or builtin

I'm having a lot of trouble with SQLiteDatabase command. I have loaded up spatialite and enabled the extension. I want one of my values to be the output of the MakePoint function so I have a content value like this:
values.put("Location", "MakePoint(43.2, 27.345, 4326)");
When this is passed into SQLiteDatabase.Update() it's escapsed so that the resulting string ends up being "UPDATE Targets SET Location='MakePoint(43.2, 27.345, 4326)'" SQLite hates this and throws an exception.
Is there an easy way around this? Right now I'm trying to build up the string manually since I can't use Update.
SQLiteDatabase was not designed for the SpatiaLite extension.
update() supports only simple values, not generic expressions; you cannot use it to insert geographic objects.
The only way to execute your command is to build it manually an run it through execSQL().
You could write your own database wrapper object that understands SpatiaLite data types.
I figured it out. I have to do a "SELECT MakePoint(43.2, 27.345, 4326)" first and then insert that into Location.

Android Sqlite - update multiple rows with multiple values

I need to update multiple rows in sqlite where each row gets a different value.
Currently I'm just looping on the update statements. (I'm using SQLStatement and replacing the parameters).
I know that with MySql there is an option to use the CASE command to execute one update that will update all rows with the matching values.
Is there any similar thing in sqlite?
Thanks.
If you know how your query should look like you can use either query() or rawQuery() methods instead of update

Problem while updating table in android

I created a table in the database that has the data like this:
Now i have written a query that updates the contact field by concatinating name and email fields:
UPDATE MyContacts SET contact=(SELECT name||'--'||email FROM MyContacts);
Here the problem is after executing the query the table is as below:
Why is it happening like this? In oracle i never faced this problem. Please help me. Thank you
Right now you're not specifying the correct row to retrieve the values from. Try something like this:
UPDATE MyContacts SET contact = name||'--'||email;
EDIT: Glad it worked. Your first issue was that your sub-select uses a SELECT statement with no WHERE clause (SELECT name||'--'||email FROM MyContacts will return 3 rows). One possible solution would be for SQLite to throw an error and say You've tried to set a column to the result of an expression that returns more than 1 row: I've seen this with MySQL and SQL Server. However, in this case SQLite appears to just use only the very first value returned. However, your second error then kicks in: since you don't narrow your UPDATE statement with a WHERE clause, it uses that first value returned to update EVERY single row, which is what you see.

Android Notepad Tutorial extension: getting more out of the (SQL) NotesDatabase

Confused beginner here.
I'm extending the functionality of the android notepad tutorial program. I can successfully get the data from the sql to display in a list view and I can use the findNote function to get an individual note.
What I want to be able to do is extract an individual note AND all following notes in the table. I'd be happy with some way to iterate through the remaining notes (a puzzle for me because the rowIds are not sequential) but would also settle for designing a query that... I don't know, returns a String[] with the item with the matching id at position 0 and all subsequent items later in the array.
I'm sure there are a thousand ways to do this, I'm willing to take almost any of them. Please let me know if I need to clarify further.
Just looking at the NotesDbAdapter class in that tutorial, there doesn't appear to be any timestamp captured with the notes. You would need to extend the table structure to include this and update the createNote and updateNote methods to set that accordingly. Then you can write your SQL based upon that timestamp.
John

Using sqlite to dynamically create tables in android

So my fundamentals of creating and manipulating databases are a bit messed up. My aim here is that whenever the app is launched, the user is allowed to specify a table name, and whatever data is then collected is put into that table.
However, I'm confused as to how to do this. Do I simply pass the value of a user entered variable as the table name in my contentprovider class and execute sqlite statements to create it?
I've read/reading the documentation already, so if anyone has any insight or clarity, or even better, code snippets, it would be great.
Why not simply use one table, and create a value that stands for the current app-session, and insert that value with each row. This would make your code simpler, and would still allow you to segregate/filter out the values from a particular app-session. If you want to give the user the ability to enter the value (as you are giving them the ability to choose the table name) you'd just want to check to see if that value had already been used, just as you would have to see if the table-name had already been used.

Categories

Resources