Creating a Python Database with SqLite
First we need to download Sqlite from our computer
Windows: http: //sqlitebrowser.org/ (download the exe file from here and install it like a regular file)
Ubuntu: sudo apt-get install sqlitebrowser
GNU / Linux: already available on their computers.
For friends who use Ubuntu, they will need a few more codes
1- sudo apt-get install build-essential cmake libqt4-dev libsqlite3-dev
2- tar zxvf sqlitebrowser-3.7.0.tar.gz
3- cd sqlitebrowser-3.7.0
4- cmake
5- make
6- sudo make install
so you can use the following code to test that they have installed the program
| sqlitebrowser
If the window opened, it means everything is on the way. But if it did not open, I would advise you to leave it with the top job. If you still get an error,
Database Creation
To create our first verb, we have a constantly unchanging code for a long time
| import sqlite3
That's how we included SQLite.
You can then create a database with the following line to create your first database
| vt = sqlite3.connect('utopian.sqlite')
The connect method is a method of connecting to / creating a database. If there is no such database, a new database is opened. That is, both the connect method and the method of creating a new database or connecting to an existing database. We did not have a database called Utopian.sqlite, so we opened a new database.
Now that we have opened the database, we need to perform an operation on this database, so we use the cursor method to log in and select the database.
| sv = vt.cursor()
I know I set it to sv variable so you can assign the variable you want. After we used this code, we did it to all of them.
With a new method execute method, we can create a table in our database so you can create your own table
| sv.execute("CREATE TABLE utopianio (name,last name)")
We have created a table called utupianio and we have opened up columns called noun and surname. And now we come to the table to enter data
again using the execute method, but this time let's make a change in terms of convenience and readability. In the table below, we enter the values in the utopianio tablonus respectively in the order of first name, then surname.
| data_gir = """INSERT INTO utopianio VALUES ('john', 'brayn')"""
sv.execute(data_gir)<
what we have done here now is that we added John to the noun part of Brayn into the utopianio tabl and added a variable. Then I printed my database using the execute method.
and now we need to handle these operations on the database with the above codes We want to print something but now we will do the processing part, that is, the combining part.
In this regard, we will help the commit method. We can process your database with the following line.
| sv.commit()
Now that we have done our operations we have done everything to do the processing according to the principles we will need to close the database in it will use the close method.
| sv.close()
so we shut down our database.
Creating the database up until now Creating the table Create the tablespace on the table, learn how to enter values into the columns sequentially, process the data into the database, and finally close it. I will explain how to print in this database immediately.
In this regard, our fetchall method will help.
| data = sv.fetchall()
| print(data)<
a screen like this will have to appear
If we get such an output, it means that you have done everything perfectly. friends in this writing I want to log in to Sqlite I want to create database, table, column, do not enter data and print the data other than writing to the one-shot file. I will write an application for you that will be related to this sqlite in the near future. If you have a point in cheerfulness, you can write something like this in an error situation or something.
Codes :
import sqlite3
vt = sqlite3.connect('utopian.sqlite')
sv = vt.cursor()
sv.execute("CREATE TABLE utopianio (name, last name)")
data_gir = """INSERT INTO utopianio VALUES ('John', 'Brayn')"""
sv.execute(data_gir)
sv.commit()
data = sv.fetchall()
print(data)<
Posted on Utopian.io - Rewarding Open Source Contributors
Your contribution cannot be approved because it does not refer to or relate to an open-source repository. See here for a definition of "open-source."
You can contact us on Discord.
[utopian-moderator]
Congratulations @descard63! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP