How to insert values into sqlite3 using autohotkey - sqlite

My problem is I am unable to insert values into sqlite3 using autohot key.
My code is:
MyName := name
myTel := telphonenumber
$sSQL := "insert into myTable Values ('" %MyName% "', " %myTel% ");"
and I also tried with ,
$sSQL := "insert into myTable Values ( ' " . MyName . " ', " . myTel . ");"
But neither of these works.
Any suggestion is appreciated..

I've not used AHK with SQLite before, but is it just the query you're having issues with? Try this (note the lack of the colon before the equals sign):
$sSQL = "insert into myTable Values ('%MyName%', '%myTel%');"
Your second attempt produces a query that is technically valid, but it would put a space either side of the name in the database ('John' would be ' John '). Also I'm guessing you don't really want to be using a numeric field in your database for a telephone number? If a number begins with 0 or is to large you could have issues. The version above will insert it as a string.

MyName := name
myTel := telphonenumber
$sSQL := "insert into myTable Values ('" MyName "', '" myTel "');"
I prefer to put the phone number also into apostrophe.
if you do not have a phone number then there is no error:
insert into myTable Values ('John', '' );
and as Gary said right: "If a number begins with 0 or ... issues."
the select of the telephone number 0177... will give you later 177...
better you also use a string for the phone number and not an number format.
create table myTable
(
name TEXT
phone TEXT
) ;

Related

SQLite custom functions as match string

I create a SQLite function which takes a string and returns another string, then I can use the return value as match strings. Code is here
It works very well except for the single quotes. In this case, it can't match any rows, but if I directly use the returned string, it can match. Anyone know what's the problem here?
sqlite> select simple_query('''');
"''"
sqlite> select ' ', simple_highlight(t1, 0, '[', ']') from t1 where x match simple_query('''');
sqlite> select ' ', simple_highlight(t1, 0, '[', ']') from t1 where x match '"''"';
|#English &special _characters."[']bacon-&and[']-eggs%
Full example here
This question finally answered in sqlite-forum, and I'd like to post the reason in here.
The reason is SQLite will try to escape the string for us, we can verify that after turn on quote mode, as you can see, our return value will be escaped from "'" to "''" by SQLite. Which means we don't need to escape single quote in our function.
sqlite> select simple_query('''');
"'"
sqlite> select simple_query('"');
""""
sqlite> .mode quote
sqlite> select simple_query('"');
'""""'
sqlite> select simple_query('''');
'"''"'

Replace new line from the end of string SQL and update columns

I have a table called Players with two columns Name and PlayerID. I am using SQLite under DB Browser for SQLite.
Unfortunately, all my player's names have a something like a "\n" (a newline) at the end of their Name.
Ex:
"Mark
"
I tried to use Update & Replace for all the names with the following query (I have like 450 rows in the table):
UPDATE Players
SET Name = REPLACE(Name,CHAR(10),'')
WHERE PlayerID <= 500
When I execute something like:
SELECT * FROM Players
WHERE Players.Name LIKE 'Mark'
it'll return no rows because of the end line. Here 'Mark' has no "\n", so it won't be found.
If I execute:
SELECT * FROM Players
WHERE Players.Name LIKE 'Mark
'
it will return my player. (after Mark I pressed enter)
I want to change all my rows from this format
"Mark
"
to this
"Mark"
and save all the changes.
How can I solve my problem? What's wrong?
Solution
The problem was that I had /r at the end of each string, not \n. So I had to use CHAR(13) instead of CHAR(10).
UPDATE Players
SET Name = REPLACE(Name, CHAR(13), '')
Also to remove all line feed characters (\n) I used:
UPDATE Players
SET Name = REPLACE(Name, CHAR(10), '')
Moreover to remove all the spaces () I used:
UPDATE Players
SET Name = REPLACE(Name, ' ', '')

SQLite Select...Where statement "no such column" error

I have the following SQLite statement:
"SELECT * FROM HISTORICALPICKS WHERE BETTYPE = " + betType.ToString().ToUpper() + " ORDER BY DATETIME ASC"
And betType.ToString().ToUpper() is the string "ATS"
For some reason I get the following error:
System.Data.SQLite.SQLiteException : SQL logic error or missing database
no such column: ATS
...which leaves me baffled. ATS is not a column. BETTYPE is the column. I want it to select rows that have "ATS" in the BETTYPE column.
My hunch is that something is wrong with the syntax. Here is a picture showing the table name and column name, along with a highlight of the value ATS:
You need single quotes around the value.
"SELECT * FROM HISTORICALPICKS WHERE BETTYPE = '" + betType.ToString().ToUpper() + "' ORDER BY DATETIME ASC"

how to use scope_identity in sqlite

I have issue when i use scop_identity in sqlite it showing the error like near "select": syntax error...i write query like this
string txtSQLQuery = "insert into SerpTrak_Site (SiteName) values ('" + txturl.Text + "')select scope_identity();";
any wrong in this query please help me...
SQLite does not have a function by the name scope_identity
You are probably looking for 'SELECT last_insert_rowid()'
See also this question: Does SQLite support SCOPE_IDENTITY?
Missing a semicolon between the two queries.
string txtSQLQuery = "insert into SerpTrak_Site (SiteName)
values ('" + txturl.Text + "'); select scope_identity();";

SQLite add Primary Key

I created a table in Sqlite by using the CREATE TABLE AS syntax to create a table based on a SELECT statement. Now this table has no primary key but I would like to add one.
Executing ALTER TABLE table_name ADD PRIMARY KEY(col1, col2,...) gives a syntax error "near PRIMARY"
Is there a way to add a primary key either during table creation or afterwards in Sqlite?
By "during creation" I mean during creation with CREATE TABLE AS.
You can't modify SQLite tables in any significant way after they have been created. The accepted suggested solution is to create a new table with the correct requirements and copy your data into it, then drop the old table.
here is the official documentation about this: http://sqlite.org/faq.html#q11
As long as you are using CREATE TABLE, if you are creating the primary key on a single field, you can use:
CREATE TABLE mytable (
field1 TEXT,
field2 INTEGER PRIMARY KEY,
field3 BLOB,
);
With CREATE TABLE, you can also always use the following approach to create a primary key on one or multiple fields:
CREATE TABLE mytable (
field1 TEXT,
field2 INTEGER,
field3 BLOB,
PRIMARY KEY (field2, field1)
);
Reference: http://www.sqlite.org/lang_createtable.html
This answer does not address table alteration.
I tried to add the primary key afterwards by changing the sqlite_master table directly.
This trick seems to work.
It is a hack solution of course.
In short: create a regular (unique) index on the table, then make the schema writable and change the name of the index to the form reserved by sqlite to identify a primary key index, (i.e. sqlite_autoindex_XXX_1, where XXX is the table name) and set the sql string to NULL. At last change the table definition itself.
One pittfal: sqlite does not see the index name change until the database is reopened. This seems like a bug, but not a severe one (even without reopening the database, you can still use it).
Suppose the table looks like:
CREATE TABLE tab1(i INTEGER, j INTEGER, t TEXT);
Then I did the following:
BEGIN;
CREATE INDEX pk_tab1 ON tab1(i,j);
pragma writable_schema=1;
UPDATE sqlite_master SET name='sqlite_autoindex_tab1_1',sql=null WHERE name='pk_tab1';
UPDATE sqlite_master SET sql='CREATE TABLE tab1(i integer,j integer,t text,primary key(i,j))' WHERE name='tab1';
COMMIT;
Some tests (in sqlite shell):
sqlite> explain query plan select * from tab1 order by i,j;
0|0|0|SCAN TABLE tab1 USING INDEX sqlite_autoindex_tab1_1
sqlite> drop index sqlite_autoindex_tab1_1;
Error: index associated with UNIQUE or PRIMARY KEY constraint cannot be dropped
According to the sqlite docs about table creation, using the create table as select produces a new table without constraints and without primary key.
However, the documentation also says that primary keys and unique indexes are logically equivalent (see constraints section):
In most cases, UNIQUE and PRIMARY KEY constraints are implemented by creating a unique index in the database. (The exceptions are INTEGER PRIMARY KEY and PRIMARY KEYs on WITHOUT ROWID tables.) Hence, the following schemas are logically equivalent:
CREATE TABLE t1(a, b UNIQUE);
CREATE TABLE t1(a, b PRIMARY KEY);
CREATE TABLE t1(a, b);
CREATE UNIQUE INDEX t1b ON t1(b);
So, even if you cannot alter your table definition through SQL alter syntax, you can get the same primary key effect through the use a unique index.
Also, any table (except those created without the rowid syntax) have an inner integer column known as "rowid". According to the docs, you can use this inner column to retrieve/modify record tables.
You can do it like this:
CREATE TABLE mytable (
field1 text,
field2 text,
field3 integer,
PRIMARY KEY (field1, field2)
);
sqlite> create table t(id integer, col2 varchar(32), col3 varchar(8));
sqlite> insert into t values(1, 'he', 'ha');
sqlite>
sqlite> create table t2(id integer primary key, col2 varchar(32), col3 varchar(8));
sqlite> insert into t2 select * from t;
sqlite> .schema
CREATE TABLE t(id integer, col2 varchar(32), col3 varchar(8));
CREATE TABLE t2(id integer primary key, col2 varchar(32), col3 varchar(8));
sqlite> drop table t;
sqlite> alter table t2 rename to t;
sqlite> .schema
CREATE TABLE IF NOT EXISTS "t"(id integer primary key, col2 varchar(32), col3 varchar(8));
Introduction
This is based on Android's java and it's a good example on changing the database without annoying your application fans/customers. This is based on the idea of the SQLite FAQ page
http://sqlite.org/faq.html#q11
The problem
I did not notice that I need to set a row_number or record_id to delete a single purchased item in a receipt, and at same time the item barcode number fooled me into thinking of making it as the key to delete that item. I am saving a receipt details in the table receipt_barcode. Leaving it without a record_id can mean deleting all records of the same item in a receipt if I used the item barcode as the key.
Notice
Please understand that this is a copy-paste of my code I am work on at the time of this writing. Use it only as an example, copy-pasting randomly won't help you. Modify this first to your needs
Also please don't forget to read the comments in the code .
The Code
Use this as a method in your class to check 1st whether the column you want to add is missing . We do this just to not repeat the process of altering the table receipt_barcode.
Just mention it as part of your class. In the next step you'll see how we'll use it.
public boolean is_column_exists(SQLiteDatabase mDatabase , String table_name,
String column_name) {
//checks if table_name has column_name
Cursor cursor = mDatabase.rawQuery("pragma table_info("+table_name+")",null);
while (cursor.moveToNext()){
if (cursor.getString(cursor.getColumnIndex("name")).equalsIgnoreCase(column_name)) return true;
}
return false;
}
Then , the following code is used to create the table receipt_barcode if it already does NOT exit for the 1st time users of your app. And please notice the "IF NOT EXISTS" in the code. It has importance.
//mDatabase should be defined as a Class member (global variable)
//for ease of access :
//SQLiteDatabse mDatabase=SQLiteDatabase.openOrCreateDatabase(dbfile_path, null);
creation_query = " CREATE TABLE if not exists receipt_barcode ( ";
creation_query += "\n record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query += "\n rcpt_id INT( 11 ) NOT NULL,";
creation_query += "\n barcode VARCHAR( 255 ) NOT NULL ,";
creation_query += "\n barcode_price VARCHAR( 255 ) DEFAULT (0),";
creation_query += "\n PRIMARY KEY ( record_id ) );";
mDatabase.execSQL(creation_query);
//This is where the important part comes in regarding the question in this page:
//adding the missing primary key record_id in table receipt_barcode for older versions
if (!is_column_exists(mDatabase, "receipt_barcode","record_id")){
mDatabase.beginTransaction();
try{
Log.e("record_id", "creating");
creation_query="CREATE TEMPORARY TABLE t1_backup(";
creation_query+="record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query+="rcpt_id INT( 11 ) NOT NULL,";
creation_query+="barcode VARCHAR( 255 ) NOT NULL ,";
creation_query+="barcode_price VARCHAR( 255 ) NOT NULL DEFAULT (0) );";
mDatabase.execSQL(creation_query);
creation_query="INSERT INTO t1_backup(rcpt_id,barcode,barcode_price) SELECT rcpt_id,barcode,barcode_price FROM receipt_barcode;";
mDatabase.execSQL(creation_query);
creation_query="DROP TABLE receipt_barcode;";
mDatabase.execSQL(creation_query);
creation_query="CREATE TABLE receipt_barcode (";
creation_query+="record_id INTEGER PRIMARY KEY AUTOINCREMENT,";
creation_query+="rcpt_id INT( 11 ) NOT NULL,";
creation_query+="barcode VARCHAR( 255 ) NOT NULL ,";
creation_query+="barcode_price VARCHAR( 255 ) NOT NULL DEFAULT (0) );";
mDatabase.execSQL(creation_query);
creation_query="INSERT INTO receipt_barcode(record_id,rcpt_id,barcode,barcode_price) SELECT record_id,rcpt_id,barcode,barcode_price FROM t1_backup;";
mDatabase.execSQL(creation_query);
creation_query="DROP TABLE t1_backup;";
mDatabase.execSQL(creation_query);
mdb.setTransactionSuccessful();
} catch (Exception exception ){
Log.e("table receipt_bracode", "Table receipt_barcode did not get a primary key (record_id");
exception.printStackTrace();
} finally {
mDatabase.endTransaction();
}
I had the same problem and the best solution I found is to first create the table defining primary key and then to use insert into statement.
CREATE TABLE mytable (
field1 INTEGER PRIMARY KEY,
field2 TEXT
);
INSERT INTO mytable
SELECT field1, field2
FROM anothertable;
CREATE TABLE tablename(
Column1 INTEGER PRIMARY KEY,
Column2 VARCHAR(50)
)
INSERT INTO tablename
SELECT Column1 , Column2
FROM ANOTHERTABLE
I used the CREATE TABLE AS syntax to merge several columns and encountered the same problem. Here is an AppleScript I wrote to speed the process up.
set databasePath to "~/Documents/Databases/example.db"
set tableOne to "separate" -- Table from which you are pulling data
set tableTwo to "merged" -- Table you are creating
set {tempCol, tempColEntry, permColEntry} to {{}, {}, {}}
set permCol to {"id integer primary key"}
-- Columns are created from single items AND from the last item of a list
-- {{"a", "b", "c"}, "d", "e"} Columns "a" and "b" will be merged into a new column "c". tableTwo will have columns "c", "d", "e"
set nonCoal to {"City", "Contact", "Names", {"Address 1", "Address", "address one", "Address1", "Text4", "Address 1"}, {"E-Mail", "E-Mail Address", "Email", "Email Address", "EmailAddress", "Email"}, {"Zip", "Zip Code", "ZipCode", "Zip"}, {"Telephone", "BusinessPhone", "Phone", "Work Phone", "Telephone"}, {"St", "State", "State"}, {"Salutation", "Mr/Ms", "Mr/s", "Salutations", "Sautation", "Salutation"}}
-- Build the COALESCE statements
repeat with h from 1 to count of nonCoal
set aColumn to item h of nonCoal
if class of aColumn is not list then
if (count of words of aColumn) > 1 then set aColumn to quote & aColumn & quote
set end of tempCol to aColumn
set end of permCol to aColumn
else
set coalEntry to {}
repeat with i from 1 to count of aColumn
set coalCol to item i of aColumn as string
if (count of words of coalCol) > 1 then set coalCol to quote & coalCol & quote
if i = 1 then
set end of coalEntry to "TRIM(COALESCE(" & coalCol & ", '') || \" \" || "
else if i < ((count of aColumn) - 1) then
set end of coalEntry to "COALESCE(" & coalCol & ", '') || \" \" || "
else if i = ((count of aColumn) - 1) then
set as_Col to item (i + 1) of aColumn as string
if (count of words of as_Col) > 1 then set as_Col to quote & as_Col & quote
set end of coalEntry to ("COALESCE(" & coalCol & ", '')) AS " & as_Col) & ""
set end of permCol to as_Col
end if
end repeat
set end of tempCol to (coalEntry as string)
end if
end repeat
-- Since there are ", '' within the COALESCE statement, you can't use "TID" and "as string" to convert tempCol and permCol for entry into sqlite3. I rebuild the lists in the next block.
repeat with j from 1 to count of tempCol
if j < (count of tempCol) then
set end of tempColEntry to item j of tempCol & ", "
set end of permColEntry to item j of permCol & ", "
else
set end of tempColEntry to item j of tempCol
set end of permColEntry to item j of permCol
end if
end repeat
set end of permColEntry to ", " & item (j + 1) of permCol
set permColEntry to (permColEntry as string)
set tempColEntry to (tempColEntry as string)
-- Create the new table with an "id integer primary key" column
set createTable to "create table " & tableTwo & " (" & permColEntry & "); "
do shell script "sqlite3 " & databasePath & space & quoted form of createTable
-- Create a temporary table and then populate the permanent table
set createTemp to "create temp table placeholder as select " & tempColEntry & " from " & tableOne & "; " & "insert into " & tableTwo & " select Null, * from placeholder;"
do shell script "sqlite3 " & databasePath & space & quoted form of createTemp
--export the new table as a .csv file
do shell script "sqlite3 -header -column -csv " & databasePath & " \"select * from " & tableTwo & " ; \"> ~/" & tableTwo & ".csv"

Resources