<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.luntti.net/index.php?action=history&amp;feed=atom&amp;title=AIS_Save_to_database_using_Python</id>
	<title>AIS Save to database using Python - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.luntti.net/index.php?action=history&amp;feed=atom&amp;title=AIS_Save_to_database_using_Python"/>
	<link rel="alternate" type="text/html" href="https://wiki.luntti.net/index.php?title=AIS_Save_to_database_using_Python&amp;action=history"/>
	<updated>2026-07-15T08:05:56Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.40.0</generator>
	<entry>
		<id>https://wiki.luntti.net/index.php?title=AIS_Save_to_database_using_Python&amp;diff=16090&amp;oldid=prev</id>
		<title>Mol: /* Introduction */</title>
		<link rel="alternate" type="text/html" href="https://wiki.luntti.net/index.php?title=AIS_Save_to_database_using_Python&amp;diff=16090&amp;oldid=prev"/>
		<updated>2026-05-29T14:15:20Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Introduction&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 17:15, 29 May 2026&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l2&quot;&gt;Line 2:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 2:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Use ais_rtl and python to save data to db&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Use ais_rtl and python to save data to db&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;The following code will do the trick&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&amp;lt;code&gt;rtl_ais -n 2&gt;&amp;amp;1 | python -u ais_logger.py&amp;lt;/code&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Python ==&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;== Python ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mol</name></author>
	</entry>
	<entry>
		<id>https://wiki.luntti.net/index.php?title=AIS_Save_to_database_using_Python&amp;diff=16089&amp;oldid=prev</id>
		<title>Mol: Created page with &quot;== Introduction ==  Use ais_rtl and python to save data to db  == Python ==  &lt;syntaxhighlight lang=&quot;python&quot;&gt; import sys import sqlite3 from pyais.stream import IterMessages from datetime import datetime, timezone  DB = &quot;ais.db&quot; conn = sqlite3.connect(DB) cur = conn.cursor() print(&quot;AIS logger started&quot;, flush=True)  def clean_stream():     for line in sys.stdin.buffer:         if not line:             continue          line = line.strip()         if not line:             c...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.luntti.net/index.php?title=AIS_Save_to_database_using_Python&amp;diff=16089&amp;oldid=prev"/>
		<updated>2026-05-29T14:14:26Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Introduction ==  Use ais_rtl and python to save data to db  == Python ==  &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; import sys import sqlite3 from pyais.stream import IterMessages from datetime import datetime, timezone  DB = &amp;quot;ais.db&amp;quot; conn = sqlite3.connect(DB) cur = conn.cursor() print(&amp;quot;AIS logger started&amp;quot;, flush=True)  def clean_stream():     for line in sys.stdin.buffer:         if not line:             continue          line = line.strip()         if not line:             c...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Use ais_rtl and python to save data to db&lt;br /&gt;
&lt;br /&gt;
== Python ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import sys&lt;br /&gt;
import sqlite3&lt;br /&gt;
from pyais.stream import IterMessages&lt;br /&gt;
from datetime import datetime, timezone&lt;br /&gt;
&lt;br /&gt;
DB = &amp;quot;ais.db&amp;quot;&lt;br /&gt;
conn = sqlite3.connect(DB)&lt;br /&gt;
cur = conn.cursor()&lt;br /&gt;
print(&amp;quot;AIS logger started&amp;quot;, flush=True)&lt;br /&gt;
&lt;br /&gt;
def clean_stream():&lt;br /&gt;
    for line in sys.stdin.buffer:&lt;br /&gt;
        if not line:&lt;br /&gt;
            continue&lt;br /&gt;
&lt;br /&gt;
        line = line.strip()&lt;br /&gt;
        if not line:&lt;br /&gt;
            continue&lt;br /&gt;
&lt;br /&gt;
        # AIS sentences always start with !&lt;br /&gt;
        if not line.startswith(b&amp;quot;!&amp;quot;):&lt;br /&gt;
            continue&lt;br /&gt;
&lt;br /&gt;
        yield line&lt;br /&gt;
&lt;br /&gt;
stream = IterMessages(clean_stream())&lt;br /&gt;
&lt;br /&gt;
for msg in stream:&lt;br /&gt;
    try:&lt;br /&gt;
        data = msg.decode().asdict()&lt;br /&gt;
        print( data )&lt;br /&gt;
        ts = datetime.now(timezone.utc).isoformat()&lt;br /&gt;
&lt;br /&gt;
        cur.execute(&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
            INSERT INTO ais_data (ts, mmsi, msg_type, lat, lon, sog, cog, heading, shipname, raw)&lt;br /&gt;
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;, (ts,data.get(&amp;quot;mmsi&amp;quot;),data.get(&amp;quot;msg_type&amp;quot;),data.get(&amp;quot;lat&amp;quot;),data.get(&amp;quot;lon&amp;quot;),data.get(&amp;quot;speed&amp;quot;),data.get(&amp;quot;course&amp;quot;),data.get(&amp;quot;heading&amp;quot;),data.get(&amp;quot;shipname&amp;quot;),str(msg)  ))&lt;br /&gt;
&lt;br /&gt;
        conn.commit()&lt;br /&gt;
&lt;br /&gt;
        print(&amp;quot;INSERTED:&amp;quot;, data.get(&amp;quot;mmsi&amp;quot;), flush=True)&lt;br /&gt;
&lt;br /&gt;
    except Exception as e:&lt;br /&gt;
        print(&amp;quot;ERROR:&amp;quot;, e, flush=True)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Mol</name></author>
	</entry>
</feed>