<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=SQLite</id>
	<title>SQLite - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://entorb.net//wiki/index.php?action=history&amp;feed=atom&amp;title=SQLite"/>
	<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=SQLite&amp;action=history"/>
	<updated>2026-05-06T11:29:52Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>https://entorb.net//wiki/index.php?title=SQLite&amp;diff=4899&amp;oldid=prev</id>
		<title>Torben at 20:44, 30 October 2024</title>
		<link rel="alternate" type="text/html" href="https://entorb.net//wiki/index.php?title=SQLite&amp;diff=4899&amp;oldid=prev"/>
		<updated>2024-10-30T20:44:32Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:Coding]][[Category:Python]]&lt;br /&gt;
===Basics===&lt;br /&gt;
SQLite natively supports the following types: NULL, INTEGER, REAL, TEXT, BLOB.&lt;br /&gt;
Additinally date works as well&lt;br /&gt;
&lt;br /&gt;
====Date and Time====&lt;br /&gt;
Use of CURRENT_TIMESTAMP variable&lt;br /&gt;
 UPDATE outbox SET date_sent = CURRENT_TIMESTAMP WHERE date_sent IS NULL&lt;br /&gt;
Date select&lt;br /&gt;
 DELETE FROM outbox WHERE date_created &amp;lt; date(&amp;#039;now&amp;#039;, &amp;#039;-1 month&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
====VACUUM: Reduce Filesize after DELETE====&lt;br /&gt;
 VACUUM&lt;br /&gt;
can also be run via sqlite3 command line tool&lt;br /&gt;
 sqlite3 grafana.db &amp;#039;VACUUM;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
===Using Python===&lt;br /&gt;
see [https://docs.python.org/3/library/sqlite3.html]&lt;br /&gt;
&lt;br /&gt;
 import sqlite3&lt;br /&gt;
 &lt;br /&gt;
 # connect to DB&lt;br /&gt;
 # con = sqlite3.connect(&amp;#039;database_file.db&amp;#039;)&lt;br /&gt;
 con = sqlite3.connect(&amp;quot;:memory:&amp;quot;)&lt;br /&gt;
 # con.row_factory = sqlite3.Row  # allows for access via row[&amp;quot;name&amp;quot;]&lt;br /&gt;
 cur = con.cursor()&lt;br /&gt;
  &lt;br /&gt;
 # create table&lt;br /&gt;
 cur.execute(&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
 CREATE TABLE points&lt;br /&gt;
    (date date, lat real, lon real, source text, name text)&lt;br /&gt;
 &amp;#039;&amp;#039;&amp;#039;)&lt;br /&gt;
 &lt;br /&gt;
 # ALTER TABLE&lt;br /&gt;
 cur.execute(&amp;quot;ALTER TABLE points ADD date_registered date&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
 # INSERT&lt;br /&gt;
 sql = &amp;quot;INSERT INTO points(date) VALUES (?)&amp;quot;&lt;br /&gt;
 cur.execute(sql, (datetime.date.today(),))  # here the &amp;quot;,&amp;quot; is required if only one entry in tuple&lt;br /&gt;
 &lt;br /&gt;
 sql = &amp;quot;INSERT INTO points(date, lat, lon) VALUES (?,?,?)&amp;quot;&lt;br /&gt;
 cur.execute(sql, (myDate, myLat, myLon))&lt;br /&gt;
 con.commit()&lt;br /&gt;
 &lt;br /&gt;
 # UPDATE&lt;br /&gt;
 sql = &amp;quot;UPDATE points SET lat = ? WHERE hash = ?&amp;quot;&lt;br /&gt;
 cur.execute(sql, (myLat, h))&lt;br /&gt;
 con.commit()&lt;br /&gt;
 &lt;br /&gt;
 # SELECT 1 row &lt;br /&gt;
 sql = &amp;quot;SELECT date FROM points WHERE hash = ? LIMIT 1&amp;quot;&lt;br /&gt;
 row = cur.execute(sql, (h,)).fetchone()&lt;br /&gt;
 &lt;br /&gt;
 # SELECT multiple rows&lt;br /&gt;
 sql = &amp;quot;SELECT date, lat, lon FROM points ORDER BY email&amp;quot;&lt;br /&gt;
 for row in cur.execute(sql):&lt;br /&gt;
     print (row[0])&lt;br /&gt;
     print (row[&amp;quot;date&amp;quot;]) # if &amp;quot;con.row_factory = sqlite3.Row&amp;quot; is used&lt;br /&gt;
 &lt;br /&gt;
 cur.close()&lt;br /&gt;
 con.close()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Using PHP===&lt;br /&gt;
 $PATH = &amp;quot;/var/www/virtual/entorb/mail-daemon/outbox.db&amp;quot;;&lt;br /&gt;
 $db = new SQLite3($PATH);&lt;br /&gt;
 $db-&amp;gt;busyTimeout(10000);&lt;br /&gt;
 $db-&amp;gt;exec(&amp;#039;BEGIN&amp;#039;);&lt;br /&gt;
 $stmt = $db-&amp;gt;prepare(&amp;#039;UPDATE outbox SET date_sent = CURRENT_TIMESTAMP WHERE date_sent IS NULL AND date_created = :date_created AND send_to = :send_to AND subject = :subject&amp;#039;);&lt;br /&gt;
 $stmt-&amp;gt;bindValue(&amp;#039;:date_created&amp;#039;, $res[&amp;#039;date_created&amp;#039;]);&lt;br /&gt;
 $stmt-&amp;gt;bindValue(&amp;#039;:send_to&amp;#039;, $res[&amp;#039;send_to&amp;#039;]);&lt;br /&gt;
 $stmt-&amp;gt;bindValue(&amp;#039;:subject&amp;#039;, $res[&amp;#039;subject&amp;#039;]);&lt;br /&gt;
 $result = $stmt-&amp;gt;execute();&lt;br /&gt;
 $db-&amp;gt;exec(&amp;#039;COMMIT&amp;#039;);&lt;br /&gt;
 $db-&amp;gt;close();&lt;br /&gt;
&lt;br /&gt;
===Using Perl===&lt;br /&gt;
 use lib ( &amp;#039;/var/www/virtual/entorb/perl5/lib/perl5&amp;#039; );&lt;br /&gt;
 my $PATH = &amp;quot;/var/www/virtual/entorb/mail-daemon/outbox.db&amp;quot;;&lt;br /&gt;
 use DBI;&lt;br /&gt;
 my $dbh = DBI-&amp;gt;connect( &amp;quot;dbi:SQLite:dbname=$PATH&amp;quot;, &amp;quot;&amp;quot;, &amp;quot;&amp;quot; );&lt;br /&gt;
 $dbh-&amp;gt;{ AutoCommit } = 0;&lt;br /&gt;
 my $sth = $dbh-&amp;gt;prepare( &amp;quot;INSERT INTO outbox(send_to, subject, body, send_from, send_cc, send_bcc, date_created, date_sent) VALUES (?, ?, ?, ?, &amp;#039;&amp;#039;, &amp;#039;&amp;#039;, CURRENT_TIMESTAMP, NULL)&amp;quot; );&lt;br /&gt;
 $sth-&amp;gt;bind_param( 1, $send_to,   DBI::SQL_VARCHAR );&lt;br /&gt;
 $sth-&amp;gt;bind_param( 2, $subject,   DBI::SQL_VARCHAR );&lt;br /&gt;
 $sth-&amp;gt;bind_param( 3, $body,      DBI::SQL_VARCHAR );&lt;br /&gt;
 $sth-&amp;gt;bind_param( 4, $send_from, DBI::SQL_VARCHAR );&lt;br /&gt;
 $sth-&amp;gt;execute;&lt;br /&gt;
 $dbh-&amp;gt;commit;&lt;br /&gt;
 $dbh-&amp;gt;disconnect();&lt;/div&gt;</summary>
		<author><name>Torben</name></author>
	</entry>
</feed>