![]() ![]() We will demonstrate this in the next section. The select statement selects the required data from the database table, and if you want to fetch the selected data, the fetchall() method of the cursor object is used. If you want to select a few columns from a table, then specify the columns like the following: select column1, column2 from tables_nameįor example, cursorObj.execute('SELECT id, name FROM employees') For example, select all the columns of the employees’ table, run the following code: cursorObj.execute('SELECT * FROM employees ') In SQLite3, the SELECT statement is executed in the execute method of the cursor object. The syntax for this will be as follows: select * from table_name If you want to select all the columns of the data from a table, you can use the asterisk (*). ![]() ![]() You can use the select statement to select data from a particular table. The entire code is as follows: import sqlite3ĬursorObj.execute('INSERT INTO employees(id, name, salary, department, position, hireDate) VALUES(?, ?, ?, ?, ?, ?)', entities)Įntities = (2, 'Andrew', 800, 'IT', 'Tech', '') Where entities contain the values for the placeholders as follows: entities = (2, 'Andrew', 800, 'IT', 'Tech', '') The syntax of the INSERT will be like the following: cursorObj.execute('''INSERT INTO employees(id, name, salary, department, position, hireDate) VALUES(?, ?, ?, ?, ?, ?)''', entities) You can use the question mark (?) as a placeholder for each value. We can also pass values/arguments to an INSERT statement in the execute() method. To check if the data is inserted, click on Browse Data in the DB Browser: Consider the following line of code: cursorObj.execute("INSERT INTO employees VALUES(1, 'John', 700, 'HR', 'Manager', '')") To insert data in a table, we use the INSERT INTO statement. Open your mydatabase.db file with the program, and you should see your table: To check if our table is created, you can use the DB browser for SQLite to view your table. The commit() method saves all the changes we make. In the above code, we have defined two methods, the first one establishes a connection and the second method creates a cursor object to execute the create table statement. The code will be like this: import sqlite3ĬursorObj.execute("CREATE TABLE employees(id integer PRIMARY KEY, name text, salary real, department text, position text, hireDate text)") Let’s create employees with the following attributes: employees (id, name, salary, department, position, hireDate)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |