SQLITE3 + execute Insert [duplicate] - sqlite

This question already has answers here:
SQLite parameter substitution problem
(8 answers)
Closed 7 years ago.
Trying to execute insert an item coming from a list:`
item=u'Sunil Goyal'
c.execute('''INSERT INTO bpersons(person_name) VALUES (?)''',item)`
is simple enough, but it returns
Incorrect number of bindings supplied. The current statement uses 1, and there are 11 supplied.
Clearly instead of reading item as one element, it is reading characters. There is no problem with the earlier code which returns this list:
>>> if meta[7]:#bcoz list could be empty also
for item in meta[7]:
print item
Sunil Goyal
Rehan Yar Khan
Khan
Kae Capital
Ashish Shankar
Karthik Reddy
Feroze Azeez
len(meta[7])
7
Any idea where I am going wrong?

insert is looking for an iterable (documentation) and this succeeds because your unicode string is an iterable, but you should put it inside of a tuple or list to be handled properly by sqlite3.
c.execute('''INSERT INTO bpersons(person_name) VALUES (?)''',(item,))`

Related

What does the TEXT (5) in CREATE TABLE SQL mean when I use SQLitle? [duplicate]

This question already has answers here:
sqlite allows char(p) inputs exceeding length p
(2 answers)
Varchar Length? SQLite in Android
(1 answer)
String data type in sqlite
(2 answers)
No limitation on SQL column data type
(1 answer)
Closed 10 days ago.
The Code A is to created a SQLitle table made by SQLite Studio 3.4.3.
1: What does the TEXT (5) mean in SQLitle? Does it mean that the length of linkTitle field can't be exceed 5 letters? What will happend if I add a record with 20 letters for linkTitle field?
2: The default value "The title of saved links" of the linkTitle field has exceeded 5 letters, What will happend ?
Code A
CREATE TABLE myTable (
id INTEGER PRIMARY KEY ASC AUTOINCREMENT,
linkTitle TEXT (5) NOT NULL
DEFAULT [The title of saved links],
linkSaved TEXT (10) NOT NULL
);
It has no affect, the column type is considered as TEXT. SQLite accommodates much of the SQL that is used by other databases and hence the acceptance of the length specifier but ignoring (other than syntactically (omit either parenthesis and a syntax error will be issued, likewise if a non numeric is specified)).
No length restriction is imposed by the specifier.
If you were to use:-
INSERT INTO myTable (linkTitle,linkSaved) VALUES
('The quick brown fox jumped over the lazy fence','The slow grey elephant could not jump over the fence so crashed though it'),
(100,zeroblob(10)),
(10.1234567,'10.1234567')
;
SELECT * FROM myTable;
The result would be:-
This also demonstrates that you can save (with the exception of an alias of the rowid, e.g. the id column, or the rowid itself ), any type of data in any type of column. Which other databases typically do not allow.
Furthermore, the column type itself is highly flexible. That is you could specify virtually any type e.g. the_column ridiculoustype and SQLite will make the type NUMERIC (the default/drop through type) in this case (see 3.1 in the link for the rules for assigning a type affinity).
You should perhaps have a read of https://www.sqlite.org/datatype3.html

how to get the last part of strings with different lengths ended by ".nc" [duplicate]

This question already has answers here:
Get filename without extension in R
(9 answers)
Find file name from full file path
(4 answers)
Closed 3 years ago.
I have several download links (i.e., strings), and each string has different length.
For example let's say these fake links are my strings:
My_Link1 <- "http://esgf-data2.diasjp.net/pr/gn/v20190711/pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
My_Link2 <- "http://esgf-data2.diasjp.net/gn/v20190711/pr_-present_r1i1p1f1_gn_19500101-19591231.nc"
My goals:
A) I want to have only the last part of each string ended by .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc
pr_-present_r1i1p1f1_gn_19500101-19591231.nc
B) I want to have only the last part of each string before .nc , and get these results:
pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231
pr_-present_r1i1p1f1_gn_19500101-19591231
I tried to find a way on the net, but I failed. It seems this can be done in Python as documented here:
How to get everything after last slash in a URL?
Does anyone know the same method in R?
Thanks so much for your time.
A shortcut to get last part of the string would be to use basename
basename(My_Link1)
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231.nc"
and for the second question if you want to remove the last ".nc" we could use sub like
sub("\\.nc", "", basename(My_Link1))
#[1] "pr_day_MRI-AGCM3-2-H_highresSST_gn_20100101-20141231"
With some regex here is another way to get first part :
sub(".*/", "", My_Link1)

Problem adding an image to database using SQL [duplicate]

This question already has answers here:
How to use LOAD_FILE to load a file into a MySQL blob?
(6 answers)
Closed 4 years ago.
INSERT INTO items (id,name,image,price) VALUES('1','iphone 5s',LOAD_FILE('C:\xampp\htdocs\project\1.jpg'),300);
this statement cause error
INSERT INTO items (id,name,image,price) VALUES('1','iphone 5s',LOAD_FILE('C:\xampp\htdocs\project\1.jpg'),300)
MySQL said: Documentation
here is the error
#1048 - Column 'image' cannot be null
Please double check the file path of your "1.jpg" or maybe look at the image format whether it is .jpg or .png.
Else, try this format:
LOAD_FILE('../1.jpg')
"Different backslash position"
I do not think you should mention the C:/xampp/htdocs/project because you are already in the project folder when you run your code.
Hopefully it is working.
You must escape backslashes in any string:
... LOAD_FILE('C:\\xampp\\htdocs\\project\\1.jpg') ...
Or, since MySQL will 'correctly' interpret forward-slashes, even on Windows:
... LOAD_FILE('C:/xampp/htdocs/project/1.jpg') ...
(I assume image is declared BLOB or MEDIUMBLOB?)

Write a regular expression to match phone numbers [duplicate]

This question already has answers here:
Extract phone number regex
(3 answers)
Closed 4 years ago.
I have to write a regular expression to match phone numbers. A phone number can be in any one of the following forms:
(123) 456-7890
(123)456-7890
1234567890
123 456 7890
123-456-7890
123.456.7890
I have to store the regular expression in a variable called re5.
The test cases I have to pass are here: link
I just need the re5 = "..." part, where I need the correct code for the ... part.
This is what I have so far..
re5 = "^(\\d{3}|\\(\\d{3}\\))([ ]?\\d{3}[-]|[ ]\\".
I don't know what is going wrong. Thanks!
Different that other answers due to test cases in link above.
This passes all your tests:
^(?:\(\d{3}\)|\d{3})(?: |([-.])?)\d{3}(?(1)\1|[ -.]?)\d{4}$
Bear in mind that this uses PCRE-specific syntax, so you'll need to enable that using perl = TRUE

Why does symbol char(10) at the end of line lose? [duplicate]

This question already has an answer here:
SQLite FireDAC trailing spaces
(1 answer)
Closed 5 years ago.
In my case, I have lost a symbol $A at the end of line when I get field in Delphi. I think, Problem is in FireDac components. I use Delphi 10.1 Berlin and Sqlite (I don't know a version). When I start up program below, I have result 3!=4 in message.
This is code:
FD := TFDQuery.Create(nil);
FD.Connection := FDConnection1;
FD.ExecSQL('create table t2 (f2 text)');
FD.ExecSQL('insert into t2 values(''123''||char(10))');
FD.Open('select f2, length(f2) as l from t2');
ShowMessage(IntToStr(Length(FD.FieldByName('f2').AsString))+'!='+FD.FieldByName('l').AsString);
Last symbol $A lost.
May be somebody explain me this strange behavior.
You need to turn off the TFDQuery.FormatOptions.StrsTrim property:
Controls the removing of trailing spaces from string values and zero bytes from binary values
...
For SQLite, this property is applied to all string columns

Resources