Sqlite loop through all tables [duplicate] - ios

This question already has answers here:
SQLite Schema Information Metadata
(7 answers)
Closed 5 years ago.
I'm using swift 3 and I'd like to be able to loop through all the tables in my sqlite database. Is there an easy function to do this? I just want to retrieve the table names.

In sqlite database schema is sqlite_master table. It contains name of all the schema (table, Indexes, Views, etc).
to see a list of the tables in the SQLLite database, Command is ".tables".
and equivalent SQlite query to get all table names is :
select name from sqlite_master where type='table'
You can use SQL wrapper class for swift 3.0 to execute above query and fetch result.

Related

Unrecognizable table name in Big Query while using JOINS [duplicate]

This question already has answers here:
Unrecognized name: employees at [9:8]
(2 answers)
Closed 4 months ago.
Hi,
I am trying to use the INNER JOIN clause on Big Query to join rows from tables titled employees and departments. But it's not recognizing the table name employees. What am I doing wrong here?
I tried adding the dataset name before the table names but it didn't work.
Add an alias on each table and it should work :
my-project-new-gdac.employee_data.employees employees
my-project-new-gdac.employee_data.departments departments

Getting hash map result from sql query [duplicate]

This question already has answers here:
Nested hash in redis
(2 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
I want to get a data structure like this after the sql query:-
Users table has username and city. It has 10000 records
{
"cities_arry": {
"NY": ["john", "Mich", "Roh", "Dh", "Vir"],
"KL": ["Big", "ching", "qull"],
...
}
}
update: it is not possible to store nested hash on redis. So have to use MongoDb or some other tool.
Suppose you have a table users and you have city and username, and you want to find a data structure like above, then how would you approach
How to get faster query result to get data structure like this.
Redis doesn't support nested data structures, and specifically it doesn't support a Hash inside a Hash :) You basically have a choice between two options: either serialize the internal Hash and store it in a Hash field or use another Hash key and just keep a reference to it in a field of the outer Hash.

How to delete specific table's column using FMDB?

I am trying to delete column last_name from Persons using FMDB,
let query = "ALTER TABLE Persons DROP COLUMN last_name;"
try FMDBHelper.database.executeUpdate(query, values: nil)
But comes with error
DB Error: 1 "near "DROP": syntax error".
sqlite does not support DROP COLUMN in ALTER TABLE.
You can only rename tables and add columns.
If you need to remove columns, create a new table, copy the data there, drop the old table and rename the table to its intented name.
Reference: http://www.sqlite.org/lang_altertable.html
Please note that I flagged that your question could be duplicated, I will provide an answer to make it more clear.
I think that you are missing a point, which is: The FMDB is (as mentioned in their repo description):
This is an Objective-C wrapper around SQLite
Keep in mind that since FMDB is built on top of SQLite, it is not a limitation from the library itself; it is related to how SQLite ALTER TABLE works.
The SQLite ALTER TABLE statement is limited to rename a table and add a new column to the desired table:
SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE
command in SQLite allows the user to rename a table or to add a new
column to an existing table.
http://www.sqlite.org/lang_altertable.html
For achieving what are you looking for:
You could check the answers of Delete column from SQLite table.

Retrieve a ActiveRecord Relation with a DISTINCT column, taking the first instance of a column's sort with the same relation [duplicate]

This question already has answers here:
SQL join: selecting the last records in a one-to-many relationship
(13 answers)
Closed 6 years ago.
Having trouble with the below query:
I have a surveys table
The surveys table has a foreign key to a contact (via contact_id)
There are mutiple surveys per contact
The survey has a column called scheduled_at with time data
I want a query off Surveys with one instance per contact, where that survey has the recent most scheduled_at compared to other instances with the same contact foreign key.
While this seems like a good SQL answer, wondering if there is a cleaner ActiveRecord solution?
Try running following.
#contact.surveys.order(scheduled_at: :desc).first(5)
This will return the 5 most recent surveys of that order.
Assumption:
#contact is the an object of your Contact model

Using sqlite3 in iOS Projects

This is how I currently have my methods in my project to use sqlite queries to return NSObjects to be used in my iOS project:
in the launch of my application, each table is checked if it needs to be created
in the application, there is no DROP TABLE .. queries nor ALTER TABLE ..
My question is:
should I be checking if a table exists every time I'm going to create a sqlite3 query?
should i use CREATE TABLE IF NOT EXISTS .. vs checking if a table exists using a different query like:
SELECT name FROM sqlite_master WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%' UNION ALL SELECT name FROM sqlite_temp_master WHERE type IN ('table','view') ORDER BY 1;and iterating the names of the tables and checking if the table name exists?
I want to create the least amount of overhead. i am not using Core Data as well
Also, while updating records, i've noticed if i insert the name of a column, it takes the value of that column:

Resources