<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kamran Ahmed Khan</title>
    <description>The latest articles on DEV Community by Kamran Ahmed Khan (@kamranakhan).</description>
    <link>https://dev.to/kamranakhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F561780%2F7d8978f3-d126-40b4-a474-758c4c631ab6.jpeg</url>
      <title>DEV Community: Kamran Ahmed Khan</title>
      <link>https://dev.to/kamranakhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kamranakhan"/>
    <language>en</language>
    <item>
      <title>Python Convert JSON to SQLite</title>
      <dc:creator>Kamran Ahmed Khan</dc:creator>
      <pubDate>Fri, 27 Jan 2023 21:12:37 +0000</pubDate>
      <link>https://dev.to/kamranakhan/python-convert-json-to-sqlite-4a5n</link>
      <guid>https://dev.to/kamranakhan/python-convert-json-to-sqlite-4a5n</guid>
      <description>&lt;p&gt;Here are the detailed steps to convert JSON to SQLite using Python:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Install the required libraries:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install sqlite3
pip install json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Import the required libraries:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sqlite3
import json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Open a connection to the SQLite database:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conn = sqlite3.connect('example.db')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Open the JSON file:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;with open('example.json', 'r') as json_file:
    data = json.load(json_file)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Iterate through the JSON data and insert it into the SQLite table:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for item in data:
    conn.execute("INSERT INTO example_table (field1, field2, field3) VALUES (?, ?, ?)", 
                 (item["field1"], item["field2"], item["field3"]))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Commit the changes and close the connection:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conn.commit()
conn.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Replace 'example.db' and 'example.json' with the appropriate file name, and 'example_table' with the appropriate table name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Also, you may need to create the table before inserting the data, you can check this by using the below command&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;conn.execute("CREATE TABLE example_table (field1 text, field2 text, field3 text);")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import sqlite3
import json

conn = sqlite3.connect('example.db')
conn.execute("CREATE TABLE example_table (field1 text, field2 text, field3 text);")
with open('example.json', 'r') as json_file:
    data = json.load(json_file)
    for item in data:
        conn.execute("INSERT INTO example_table (field1, field2, field3) VALUES (?, ?, ?)", (item["field1"], item["field2"], item["field3"]))
conn.commit()
conn.close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a SQLite database file named 'example.db' in the current directory and insert the data from 'example.json' into a table named 'example_table'.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
  </channel>
</rss>
