how to connect an app to a database with python?
<p>I'm new to python, so I want to know how to store the data in a database! </p>

<p>For a sample example, I want to store informations about users :</p>

<pre><code>ent1 = input('Enter your name: ')
ent2 = input('Enter your adress: ')
ent3 = int(input('Enter your number phone: '))
</code></pre>

<p>The question is how to connect the input with the database? </p>

<p>we can take the example of the sqlite3 that is preinstalled in python! and we suppose that we have a database called user.db connected to the cursur and a table called users that contains three columns (name, adress, number_phone)</p>

<p>thanks for the help!</p>

<p>Use <code>sqllite3</code> library to perform SQL <code>insert</code> query to insert data into database. For example</p>

<pre><code>import sqlite3
conn = sqlite3.connect('user.db')

## Create table. Skip if table already exists
conn.execute('''CREATE TABLE IF NOT EXISTS users
       (name    TEXT NOT NULL,
       adress   TEXT,
       number_phone TEXT);''')

ent1 = input('Enter your name: ')
ent2 = input('Enter your adress: ')
ent3 = int(input('Enter your number phone: '))

#Insert into users table
conn.execute("INSERT INTO users (name,adress,number_phone) \
      VALUES (?,?,?)",[ent1,ent2,ent3]);
conn.commit() #don't forget commit to save data
conn.close()
</code></pre>

<p>You can also have a look at the <a href="https://docs.python.org/3/library/sqlite3.html" rel="nofollow">official documentation</a> for detailed info and API references.</p>

python database sqlite3