Duplicate Question
The question on this page has been marked as a duplicate question.
Original Question
Write a Python code to create a database file and a table named 'Student_Details with three columns, 'Roll No, 'Student Name' a...Asked by Ruhi
Write a Python code to create a database file and a table named 'Student_Details with three columns, 'Roll No, 'Student Name' and 'Student Class'. Support your code with suitable comments.
Answers
There are no human answers yet.
Answered by
Bot
To create a database file and table in Python, we can use the SQLite library, which is a built-in library in Python. Here's the code to create a database file and a table named Student_Details with three columns:
```python
import sqlite3 # Import SQLite library
# Create a connection and cursor object
conn = sqlite3.connect('mydatabase.db') # create/ connect to database file "mydatabase.db"
cur = conn.cursor() # create cursor object to run SQL queries
# Create a table
cur.execute('''CREATE TABLE Student_Details
(Roll_No INTEGER PRIMARY KEY,
Student_Name TEXT,
Student_Class TEXT)''')
# Here, we are creating a table named 'Student_Details' with three columns - Roll_No (Integer Primary Key), Student_Name (Text), and Student_Class (Text).
# Save (commit) the changes
conn.commit()
# Close the connection
conn.close()
```
In the above code:
1. We first imported the SQLite library
2. Then, we created a connection object to connect to a database file 'mydatabase.db'.
3. We created a cursor object to execute SQL queries.
4. Using the cursor object, we executed the CREATE TABLE query to create a table named 'Student_Details' with three columns.
5. Finally, we saved and closed the connection to the database file.
Note: If 'mydatabase.db' file is not present in your current working directory, a new database file with the specified name will be created by the above code.
```python
import sqlite3 # Import SQLite library
# Create a connection and cursor object
conn = sqlite3.connect('mydatabase.db') # create/ connect to database file "mydatabase.db"
cur = conn.cursor() # create cursor object to run SQL queries
# Create a table
cur.execute('''CREATE TABLE Student_Details
(Roll_No INTEGER PRIMARY KEY,
Student_Name TEXT,
Student_Class TEXT)''')
# Here, we are creating a table named 'Student_Details' with three columns - Roll_No (Integer Primary Key), Student_Name (Text), and Student_Class (Text).
# Save (commit) the changes
conn.commit()
# Close the connection
conn.close()
```
In the above code:
1. We first imported the SQLite library
2. Then, we created a connection object to connect to a database file 'mydatabase.db'.
3. We created a cursor object to execute SQL queries.
4. Using the cursor object, we executed the CREATE TABLE query to create a table named 'Student_Details' with three columns.
5. Finally, we saved and closed the connection to the database file.
Note: If 'mydatabase.db' file is not present in your current working directory, a new database file with the specified name will be created by the above code.
Submit Your Answer
We prioritize human answers over AI answers.
If you are human, and you can answer this question, please submit your answer.