![]() Here is how we use this query to list all the tables present in our database. The syntax of the query will be: SELECT name FROM sqlite_master WHERE type='table' To list the tables present in a database, query the sqlite_master table using the SELECT statement of SQL. ![]() So there is also a need to list all the tables present in a database. In a database, we can create numerous amount of tables. The output of the program is shown below. You can also choose another name for the image file. This simple program will retrieve the image from the database and save it in the disk named img2.png. Import sqlite3 conn = nnect("sample.db") conn.text_factory = str print("\n Successfully connected to the database") cur = conn.cursor() print("\n Cursor has been set up successfully") print("\n Retrieving the image") cur.execute("SELECT * FROM student") ret = cur.fetchall() for i in ret: data = i with open("img2.png","wb") as file: file.write(data) print("\n The image has been saved") cur.close() mit() conn.close() To retrieve an image from a database, fetch the row using a select statement and then access the binary data of the image into a python variable, which will be stored in an image file. Then we use that variable in the INSERT statement to insert the image in the database. In the above program, we have opened the file in binary mode and read every byte and store it in variable data. ![]() This program will insert the image into the student database that you created. Import sqlite3 conn = nnect("sample.db") conn.text_factory = str print("\n Successfully connected to the database") cur = conn.cursor() print("\n Cursor has been set up successfully") with open("img.png","rb") as file: data = file.read() python_tuple = (101,"robin",data,"90") print("\n The image has been successfully imported") print("\n Now inserting in the database") cur.execute("INSERT INTO student (id,name,images,marks) VALUES (?,?,?,?)", python_tuple) print("\n The Data has been inserted Successfully ") cur.close() mit() conn.close()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |