Hive combine column values based upon condition - hive

I was wondering if it is possible to combine column values based upon a condition. Let me explain...
Let say my data looks like this
Id name offset
1 Jan 100
2 Janssen 104
3 Klaas 150
4 Jan 160
5 Janssen 164
An my output should be this
Id fullname offsets
1 Jan Janssen [ 100, 160 ]
I would like to combine the name values from two rows where the offset of the two rows are no more apart then 1 character.
My question is if this type of data manipulation is possible with and if it is could someone share some code and explaination?
Please be gentle but this little piece of code return some what what I want...
ArrayList<String> persons = new ArrayList<String>();
// write your code here
String _previous = "";
//Sample output form entities.txt
//USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Berkowitz,PERSON,9,10660
//USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,10685
File file = new File("entities.txt");
try {
//
// Create a new Scanner object which will read the data
// from the file passed in. To check if there are more
// line to read from it we check by calling the
// scanner.hasNextLine() method. We then read line one
// by one till all line is read.
//
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
if(_previous == "" || _previous == null)
_previous = scanner.nextLine();
String _current = scanner.nextLine();
//Compare the lines, if there offset is = 1
int x = Integer.parseInt(_previous.split(",")[3]) + Integer.parseInt(_previous.split(",")[4]);
int y = Integer.parseInt(_current.split(",")[4]);
if(y-x == 1){
persons.add(_previous.split(",")[1] + " " + _current.split(",")[1]);
if(scanner.hasNextLine()){
_current = scanner.nextLine();
}
}else{
persons.add(_previous.split(",")[1]);
}
_previous = _current;
}
} catch (Exception e) {
e.printStackTrace();
}
for(String person : persons){
System.out.println(person);
}
Working of this piece sample data
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Richard,PERSON,7,2732
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,2740
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,2756
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,3093
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,3195
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Berkowitz,PERSON,9,3220
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Berkowitz,PERSON,9,10660
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,10685
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Lea,PERSON,3,10858
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Lea,PERSON,3,11063
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Ken,PERSON,3,11186
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Marottoli,PERSON,9,11234
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Berkowitz,PERSON,9,17073
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Lea,PERSON,3,17095
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Stephanie,PERSON,9,17330
USER.A-GovDocs-f83c6ca3-9585-4c66-b9b0-f4c3bd57ccf4,Putt,PERSON,4,17340
Which produces this output
Richard Marottoli
Marottoli
Marottoli
Marottoli
Berkowitz
Berkowitz
Marottoli
Lea
Lea
Ken
Marottoli
Berkowitz
Lea
Stephanie Putt
Kind regards

Load the table using below create table
drop table if exists default.stack;
create external table default.stack
(junk string,
name string,
cat string,
len int,
off int
)
ROW FORMAT DELIMITED
FIELDS terminated by ','
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
location 'hdfs://nameservice1/....';
Use below query to get your desired output.
select max(name), off from (
select CASE when b.name is not null then
concat(b.name," ",a.name)
else
a.name
end as name
,Case WHEN b.off1 is not null
then b.off1
else a.off
end as off
from default.stack a
left outer join (select name
,len+off+ 1 as off
,off as off1
from default.stack) b
on a.off = b.off ) a
group by off
order by off;
I have tested this it generates your desired result.

Related

Convert Salesforce IDs from 15 digit to 18 digit using sql code/function

We are currently pulling in data from SalesForce to SQL Database tables. There are 2 custom fields on different objects that were created for the Lead ID and a look up for which event/task is linked (this can be an account id, contact id, or lead id). Both of these are pulling over the 15 digit ID.
I am trying to find out if there is any SQL code or a SQL function that will allow me to convert that 15 digit to an 18 digit ID.
I need to have that 18 digit ID to join back to the other objects.
We have already tried using the CASESAFEID(Id) function in SalesForce, but with the API that was already set up and the visibility levels our particular ETL is not showing that field. Also, we would need to get a consultant to mess with the look up column.
I would like to take the 15 digit ID and convert it to the 18 digit code. If the SalesforceID is 0035000002tAzbu, how do I get the converted 18 digit value to be 0035000002tAzbuACC. I need to get that last 3 digits using SQL query or SQL function.
you could write a custom function in your sql database.
e.g. in snowflake, you can make a function like this
CREATE OR REPLACE FUNCTION dw.my_schema.f_sfdc_ch15_to_ch18("txt" string)
RETURNS string
LANGUAGE JAVASCRIPT
AS '
if ( txt == undefined || txt == "" || typeof txt == "undefined" || txt == null) {
return ;
} else {
var id15, id18;
if (txt.length == 18) {
return txt;
} else if (txt.length == 15) {
id15 = [txt.trim()];
} else {
return "";
}
for ( var x=0; x < id15.length; x++ ) {
var s = "";
for ( var i=0; i<3; i++) {
var f = 0;
for (var j=0; j<5; j++) {
var c = id15[x].charAt(i*5+j);
if (c>="A" && c<="Z") {
f+=1<<j;
}
}
s += "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345".charAt(f);
}
id18 = id15[x]+s;
}
}
return id18.toString();
';
and use it like this
select dw.my_schema.f_sfdc_ch15_to_ch18(id15) from mytable;
This value can be computed. Check out this flowchart.
Source: https://stackoverflow.com/a/29299786/3135974

Return values of #All of functions in podio calculation fields

I have 5 contacts connected to a company, and i am trying to sort out the ones that does not have an email with the code below.
var mailArray = #All of Email with nulls
var temp = new Array()
for (var i = 0; i < mailArray.length; i++) {
if (mailArray[i].value == null) {
temp.push("null")
}
else {
temp.push("correct")
}
}
temp.join(" ")
Right now i am just pushing the strings to make sure that the flow is correct, it however returns
null null null null null
when it should return
null correct correct null null
since the second and third contact has emails. Can anyone help me or give me a hint, as how to use the return value of the #All of function.
I just want to make sure what you are referring to on the emails. Is it an email field in the contacts app, or is it a text field that is called 'email'? If you are using an email field, then the value at your [i] index is actually another object and not just a string yet. Although it doesn't matter if you are just checking for something existing, but personally I'd like to know which contacts do have emails in the output.
If it is just a text field, then you have a problem with the conditional statement. It should be === instead of ==, but I would personally just rewrite it as !mailArray[i].
I'd recommend changing your output to be a markdown table for legability. Here's the code I ended up with:
var mailArray = #All of Email text with nulls
var nameArray = #All of Name with nulls
var temp = new Array()
var table = "Email | Contact \n --- | --- "
for (var i = 0; i < mailArray.length; i++) {
if (!mailArray[i].value){// !== null) {
temp.push("correct")
temp.push(mailArray[i])
table +="\n"+ mailArray[i] + " | " + nameArray[i]
}
else {
temp.push("null")
table +="\n"+ mailArray[i] + " | " + nameArray[i] + "\n"
temp.push(mailArray[i])
}
}
temp.join(" ")
write = table
And this outputs the following table (made 3 contacts, 2 with emails and one without):

SQLite with Android Studio, return all records selected by 2 arguments

let's assume that i have a table with columns such as:
ID SSID BSSID RSSI
1 abcd hs:hd:sd -60
2 abcd hs:hd:po -68
There are about 5000 records with the same SSID, slighltly different BSSID and the LEVEL values. My device is scanning the nearest environment for WiFi networks, therefore I know their MAC address and level of RSSI. I pick 3 with the highest value od RSSI.
First thing I would like to know if it is possible to search through the database to get all the records with the LEVEL value equal or close to 60, for instance 59,58,61.
Secondly, is there a way to query the database to return all the records with the same MAC addresses and RSSI values as from the 3 best scan result? If so, how would that query look like?
EDIT: Thanks for all the answers. What I'm trying to do now is to compare 3 scans with records stored in database with getRequiredData function. I would like to pass 2 parameters to this function, mac address and level and find records with same value for both parameters. The rawQuery seems to be fine, code is compiling but the app is crashing with the first scan. I cant find the cause of it, is it because my logic of getting these parameters is wrong or does it have something to do with query?
public Cursor getRequiredData(String mac, int level){
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("SELECT BSSID, RSSI FROM TABLE_NAME WHERE BSSID =? AND RSSI=?", new String[] {mac, level});
return res;
}
scan part:
class WifiReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
sb = new StringBuilder();
Comparator<ScanResult> comparator = new Comparator<ScanResult>() {
#Override
public int compare(ScanResult o1, ScanResult o2) {
return (o1.level>o2.level ? -1 : (o1.level==o2.level ? 0 : 1));
}
};
lista = wifiManager.getScanResults();
Collections.sort(lista, comparator);
for (int i = 0; i < lista.size(); i++) {
scanResult = wifiManager.getScanResults().get(i);
sb.append(new Integer(i + 1).toString() + ". " + (lista.get(i)).SSID + " " + (lista.get(i)).BSSID + " " + (lista.get(i)).level + "\n");
boolean isInserted = myDb.insertData(lista.get(i).SSID.toString(), lista.get(i).BSSID.toString(), lista.get(i).level);
if (isInserted = true)
Toast.makeText(MainActivity.this, "Data inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not inserted", Toast.LENGTH_LONG).show();
}
for (int i=0; i<4; i++)
{
scanResult = wifiManager.getScanResults().get(i);
match = myDb.getRequiredData(lista.get(i).BSSID.toString(), lista.get(i).level);
}
Log.i("match values: ", DatabaseUtils.dumpCursorToString(match));
txt.setText(sb);
wifiManager.startScan();
}
}
Here is what match contains:
2018-12-10 16:36:26.334 13347-13347/com.example.maciek.wifiscann I/match values:: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor#e1a86d1
0 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
1 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
2 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
3 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
4 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
5 {
BSSID=f4:c5:ed:5c:s6:20
RSSI=-69
}
<<<<<
To get the 3 rows with the closest values to 60 in column LEVEL:
SELECT * FROM tablename ORDER BY ABS(LEVEL - 60), LEVEL LIMIT 3
For the 2nd part of your question, you should provide sample data of the table. Edit:
From the sample data that you posted I don't see a column RSSI, but if it exists in the table then the SELECT statement is ok.
Change the 2nd parameter of rawQuery() to:
new String[] {mac, String.valueOf(level)}
because level is int.
In onReceive() you use myDb. I don't know how you initialize it.
If the app crashes you must copy the log, the part that identifies the problem and post it.
First thing I would like to know if it is possible to search through
the database to get all the records with the LEVEL value equal or
close to 60, for instance 59,58,61.
SELECT * FROM your_table WHERE level BETWEEN 59 AND 61;
where your_table is the respective table name.
Note if levels are negative (as per example data) then BETWEEN requires the lowest value first so it would be BETWEEN -61 AND -59.
Secondly, is there a way to query the database to return all the
records with the same MAC addresses and RSSI values as from the 3 best
scan result? If so, how would that query look like?
SELECT * FROM your_table WHERE your_mac_address_column = 'the_mac_address_value' AND RSSI = 'the_rssi_value' ORDER BY LEVEL DESC LIMIT 3
Note the above assumes that the MAC address is stored in a column (if NOT then cannot be done unless the mac address can be correlated to a column).
Assumes best LEVEL is lowest so -1 is better than -60 (if not then use ASC instead of DESC)
Again your_table, your_mac_address_column, the_mac_address_value and the_rssi_value would be replaced accordingly with actual values (note that strings should be in single quotes).

Get a particular string from an expression in a column in T-SQL

I have a column in my SQL Server database table which contains '|' (pipe) separated values.
Example:
'FirstName |testname| lastName | lastname | roll |ee097765 | 100 | end'
'FirstName |testname1| lastName | lastname1 | roll2 |ee0977652 | 1100 | end'.
I want to extract the marks only using T-SQL, where my output column will have only marks i.e. 100 only or 1100 subsequently. In Oracle we can use the
SUBSTRING_INDEX
function, but the same is not available in T-SQL.
Can anyone point me how to do this?
SUBSTRING_INDEX(SUBSTRING_INDEX(field, '|', 3), '|', -1)
declare #TEST table(val nvarchar(1000))
insert into #TEST
values ('FirstName |testname| lastName | lastname | roll |ee097765 | 100 | end'),
('FirstName |testname1| lastName | lastname1 | roll2 |ee0977652 | 1100 | end')
SELECT Value
FROM #TEST CROSS APPLY
Split(val,'|')
WHERE ISNUMERIC(Value) = 1
You just have to use a generic split function you'll find : https://codereview.stackexchange.com/questions/15125/sql-server-split-function
You could use SQLCLR function. There are some caveats in applying it to SQL Server. Anyway, here's one of the variants to find required values:
public partial class UserDefinedFunctions
{
[SqlFunction]
public static SqlString GetMarkPosition(SqlString sqlString)
{
// Assume there will be no result,
// so initialize return value to null string.
SqlString retVal = SqlString.Null;
// Extract C# string
string input = sqlString.Value;
// I use Regex Split method rather array Split method,
// since Regexe method handles spaces in elegant way.
string pattern = #"\s*\|\s*";
// Get array of values
string[] split = Regex.Split(input, pattern);
// The logic is the following:
// First, we find the marker's position in list.
// The sought value is one index less.
// This process is repeated for each marker.
// If no match is found for any marker,
// the function will just return empty string,
// as we initialized retVal variable.
foreach (string marker in new[] { "100", "1100" })
{
var list = new List<string>(split);
int index = list.IndexOf(marker);
if (index > 0)
{
retVal = new SqlString(list[index - 1]);
break;
}
}
return retVal;
}
}
In order to create this function, there are several steps you need to do.
If you're using SQL Server 2017, the security model has been changed (read, for example here and here), so Visual Studio won't help you in creating assembly on SQL Server's side - you'll have to do it manually.

How to get a list of column names on Sqlite3 database?

I want to migrate my iPhone app to a new database version. Since I don't have some version saved, I need to check if certain column names exist.
This Stackoverflow entry suggests doing the select
SELECT sql FROM sqlite_master
WHERE tbl_name = 'table_name' AND type = 'table'
and parse the result.
Is that the common way? Alternatives?
PRAGMA table_info(table_name);
will get you a list of all the column names.
If you do
.headers ON
you will get the desired result.
If you have the sqlite database, use the sqlite3 command line program and these commands:
To list all the tables in the database:
.tables
To show the schema for a given tablename:
.schema tablename
Just for super noobs like me wondering how or what people meant by
PRAGMA table_info('table_name')
You want to use use that as your prepare statement as shown below. Doing so selects a table that looks like this except is populated with values pertaining to your table.
cid name type notnull dflt_value pk
---------- ---------- ---------- ---------- ---------- ----------
0 id integer 99 1
1 name 0 0
Where id and name are the actual names of your columns. So to get that value you need to select column name by using:
//returns the name
sqlite3_column_text(stmt, 1);
//returns the type
sqlite3_column_text(stmt, 2);
Which will return the current row's column's name. To grab them all or find the one you want you need to iterate through all the rows. Simplest way to do so would be in the manner below.
//where rc is an int variable if wondering :/
rc = sqlite3_prepare_v2(dbPointer, "pragma table_info ('your table name goes here')", -1, &stmt, NULL);
if (rc==SQLITE_OK)
{
//will continue to go down the rows (columns in your table) till there are no more
while(sqlite3_step(stmt) == SQLITE_ROW)
{
sprintf(colName, "%s", sqlite3_column_text(stmt, 1));
//do something with colName because it contains the column's name
}
}
If you want the output of your queries to include columns names and be correctly aligned as columns, use these commands in sqlite3:
.headers on
.mode column
You will get output like:
sqlite> .headers on
sqlite> .mode column
sqlite> select * from mytable;
id foo bar
---------- ---------- ----------
1 val1 val2
2 val3 val4
An alternative way to get a list of column names not mentioned here that is cross platform and does not rely on the sqlite3.exe shell is to select from the PRAGMA_TABLE_INFO() table value function.
SELECT name FROM PRAGMA_TABLE_INFO('your_table');
name
tbl_name
rootpage
sql
You can check if a certain column exists by querying:
SELECT 1 FROM PRAGMA_TABLE_INFO('your_table') WHERE name='column1';
1
This is what you use if you don't want to parse the result of select sql from sqlite_master or pragma table_info.
Note this feature is experimental and was added in SQLite version 3.16.0 (2017-01-02).
Reference:
https://www.sqlite.org/pragma.html#pragfunc
To get a list of columns you can simply use:
.schema tablename
I know it is an old thread, but recently I needed the same and found a neat way:
SELECT c.name FROM pragma_table_info('your_table_name') c;
When you run the sqlite3 cli, typing in:
sqlite3 -header
will also give the desired result
.schema table_name
This will list down the column names of the table from the database.
Hope this will help!!!
you can use Like statement if you are searching for any particular column
ex:
SELECT * FROM sqlite_master where sql like('%LAST%')
This command below sets column names:
.header on
Then, this is how it looks like below:
sqlite> select * from user;
id|first_name|last_name|age
1|Steve|Jobs|56
2|Bill|Gates|66
3|Mark|Zuckerberg|38
And this command below unsets column names:
.header off
Then, this is how it looks like below:
sqlite> select * from user;
1|Steve|Jobs|56
2|Bill|Gates|66
3|Mark|Zuckerberg|38
And these commands show the details of the command ".header":
.help .header
Or:
.help header
Then, this is how it looks like below:
sqlite> .help .header
.headers on|off Turn display of headers on or off
In addition, this command below sets the output mode "box":
.mode box
Then, this is how it looks like below:
sqlite> select * from user;
┌────┬────────────┬────────────┬─────┐
│ id │ first_name │ last_name │ age │
├────┼────────────┼────────────┼─────┤
│ 1 │ Steve │ Jobs │ 56 │
│ 2 │ Bill │ Gates │ 66 │
│ 3 │ Mark │ Zuckerberg │ 38 │
└────┴────────────┴────────────┴─────┘
And, this command below sets the output mode "table":
.mode table
Then, this is how it looks like below:
sqlite> select * from user;
+----+------------+------------+-----+
| id | first_name | last_name | age |
+----+------------+------------+-----+
| 1 | Steve | Jobs | 56 |
| 2 | Bill | Gates | 66 |
| 3 | Mark | Zuckerberg | 38 |
+----+------------+------------+-----+
And these commands show the details of the command ".mode":
.help .mode
Or:
.help mode
Then, this is how it looks like below:
sqlite> .help .mode
.import FILE TABLE Import data from FILE into TABLE
Options:
--ascii Use \037 and \036 as column and row separators
--csv Use , and \n as column and row separators
--skip N Skip the first N rows of input
--schema S Target table to be S.TABLE
-v "Verbose" - increase auxiliary output
Notes:
* If TABLE does not exist, it is created. The first row of input
determines the column names.
* If neither --csv or --ascii are used, the input mode is derived
from the ".mode" output mode
* If FILE begins with "|" then it is a command that generates the
input text.
.mode MODE ?OPTIONS? Set output mode
MODE is one of:
ascii Columns/rows delimited by 0x1F and 0x1E
box Tables using unicode box-drawing characters
csv Comma-separated values
column Output in columns. (See .width)
html HTML <table> code
insert SQL insert statements for TABLE
json Results in a JSON array
line One value per line
list Values delimited by "|"
markdown Markdown table format
qbox Shorthand for "box --width 60 --quote"
quote Escape answers as for SQL
table ASCII-art table
tabs Tab-separated values
tcl TCL list elements
OPTIONS: (for columnar modes or insert mode):
--wrap N Wrap output lines to no longer than N characters
--wordwrap B Wrap or not at word boundaries per B (on/off)
--ww Shorthand for "--wordwrap 1"
--quote Quote output text as SQL literals
--noquote Do not quote output text
TABLE The name of SQL table used for "insert" mode
In order to get the column information you can use the following snippet:
String sql = "select * from "+oTablename+" LIMIT 0";
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery(sql);
ResultSetMetaData mrs = rs.getMetaData();
for(int i = 1; i <= mrs.getColumnCount(); i++)
{
Object row[] = new Object[3];
row[0] = mrs.getColumnLabel(i);
row[1] = mrs.getColumnTypeName(i);
row[2] = mrs.getPrecision(i);
}
//JUST little bit modified the answer of giuseppe which returns array of table columns
+(NSMutableArray*)tableInfo:(NSString *)table{
sqlite3_stmt *sqlStatement;
NSMutableArray *result = [NSMutableArray array];
const char *sql = [[NSString stringWithFormat:#"PRAGMA table_info('%#')",table] UTF8String];
if(sqlite3_prepare(md.database, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(#"Problem with prepare statement tableInfo %#",
[NSString stringWithUTF8String:(const char *)sqlite3_errmsg(md.database)]);
}
while (sqlite3_step(sqlStatement)==SQLITE_ROW)
{
[result addObject:
[NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)]];
}
return result;
}
.schema
in sqlite console when you have you're inside the table
it looks something like this for me ...
sqlite>.schema
CREATE TABLE players(
id integer primary key,
Name varchar(255),
Number INT,
Team varchar(255)
This is an old question, but here is an alternative answer that retrieves all the columns in the SQLite database, with the name of the associated table for each column :
WITH tables AS (SELECT name tableName, sql
FROM sqlite_master WHERE type = 'table' AND tableName NOT LIKE 'sqlite_%')
SELECT fields.name, fields.type, tableName
FROM tables CROSS JOIN pragma_table_info(tables.tableName) fields
This returns this type of result:
{
"name": "id",
"type": "integer",
"tableName": "examples"
}, {
"name": "content",
"type": "text",
"tableName": "examples"
}
For a simple table containing an identifier and a string content.
function getDetails(){
var data = [];
dBase.executeSql("PRAGMA table_info('table_name') ", [], function(rsp){
if(rsp.rows.length > 0){
for(var i=0; i<rsp.rows.length; i++){
var o = {
name: rsp.rows.item(i).name,
type: rsp.rows.item(i).type
}
data.push(o);
}
}
alert(rsp.rows.item(0).name);
},function(error){
alert(JSON.stringify(error));
});
}
-(NSMutableDictionary*)tableInfo:(NSString *)table
{
sqlite3_stmt *sqlStatement;
NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
const char *sql = [[NSString stringWithFormat:#"pragma table_info('%s')",[table UTF8String]] UTF8String];
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(#"Problem with prepare statement tableInfo %#",[NSString stringWithUTF8String:(const char *)sqlite3_errmsg(db)]);
}
while (sqlite3_step(sqlStatement)==SQLITE_ROW)
{
[result setObject:#"" forKey:[NSString stringWithUTF8String:(char*)sqlite3_column_text(sqlStatement, 1)]];
}
return result;
}
I know it's too late but this will help other.
To find the column name of the table, you should execute select * from tbl_name and you will get the result in sqlite3_stmt *. and check the column iterate over the total fetched column. Please refer following code for the same.
// sqlite3_stmt *statement ;
int totalColumn = sqlite3_column_count(statement);
for (int iterator = 0; iterator<totalColumn; iterator++) {
NSLog(#"%s", sqlite3_column_name(statement, iterator));
}
This will print all the column names of the result set.
I was able to retrieve table names with corresponding columns by using one sql query, but columns output is comma separated. I hope it helps somebody
SELECT tbl_name, (SELECT GROUP_CONCAT(name, ',') FROM PRAGMA_TABLE_INFO(tbl_name)) as columns FROM sqlite_schema WHERE type = 'table';
Get a list of tables and columns as a view:
CREATE VIEW Table_Columns AS
SELECT m.tbl_name AS TableView_Name, m.type AS TableView, cid+1 AS Column, p.*
FROM sqlite_master m, Pragma_Table_Info(m.tbl_name) p
WHERE m.type IN ('table', 'view') AND
( m.tbl_name = 'mypeople' OR m.tbl_name LIKE 'US_%') -- filter tables
ORDER BY m.tbl_name;
//Called when application is started. It works on Droidscript, it is tested
function OnStart()
{
//Create a layout with objects vertically centered.
lay = app.CreateLayout( "linear", "VCenter,FillXY" );
//Create a text label and add it to layout.
txt = app.CreateText( "", 0.9, 0.4, "multiline" )
lay.AddChild( txt );
app.AddLayout(lay);
db = app.OpenDatabase( "MyData" )
//Create a table (if it does not exist already).
db.ExecuteSql( "drop table if exists test_table" )
db.ExecuteSql( "CREATE TABLE IF NOT EXISTS test_table " +
"(id integer primary key, data text, num integer)",[],null, OnError )
db.ExecuteSql( "insert into test_table values (1,'data10',100),
(2,'data20',200),(3,'data30',300)")
//Get all the table rows.
DisplayAllRows("SELECT * FROM test_table");
DisplayAllRows("select *, id+100 as idplus, 'hahaha' as blabla from
test_table order by id desc;")
}
//function to display all records
function DisplayAllRows(sqlstring) // <-- can you use for any table not need to
// know column names, just use a *
// example:
{
//Use all rows what is in ExecuteSql (try any, it will works fine)
db.ExecuteSql( sqlstring, [], OnResult, OnError )
}
//Callback to show query results in debug.
function OnResult( res )
{
var len = res.rows.length;
var s = txt.GetText();
// ***********************************************************************
// This is the answer how to read column names from table:
for(var ColumnNames in res.rows.item(0)) s += " [ "+ ColumnNames +" ] "; // "[" & "]" optional, i use only in this demo
// ***********************************************************************
//app.Alert("Here is all Column names what Select from your table:\n"+s);
s+="\n";
for(var i = 0; i < len; i++ )
{
var rows = res.rows.item(i)
for (var item in rows)
{
s += " " + rows[item] + " ";
}
s+="\n\n";
}
//app.Alert(s);
txt.SetText( s )
}
//Callback to show errors.
function OnError( msg )
{
app.Alert( "Error: " + msg )
}
If you're using the SQLite3, INFORMATION_SCHEMA is not supported. Use PRAGMA table_info instead. This will return 6 rows of information about the table. To fetch the column name (row2), use a for loop like the following
cur.execute("PRAGMA table_info(table_name)") # fetches the 6 rows of data
records = cur.fetchall()
print(records)
for row in records:
print("Columns: ", row[1])
For use in Python with sqlite3
Top answer PRAGMA table_info() returns a list of tuples, which might not be suitable for further processing, e.g.:
[(0, 'id', 'INTEGER', 0, None, 0),
(1, 'name', 'TEXT', 0, None, 0),
(2, 'age', 'INTEGER', 0, None, 0),
(3, 'profession', 'TEXT', 0, None, 0)]
When using sqlite3 in Python, simply add a list comprehension in the end to filter out unwanted information.
import sqlite3 as sq
def col_names(t_name):
with sq.connect('file:{}.sqlite?mode=ro'.format(t_name),uri=True) as conn:
cursor = conn.cursor()
cursor.execute("PRAGMA table_info({}) ".format(t_name))
data = cursor.fetchall()
return [i[1] for i in data]
col_names("your_table_name")
Result
["id","name","age","profession"]
DISCLAIMER: Do not use in production as this snippet is subject to possible SQL injection!