From the course: Master Next.js by Building Scalable Apps with Routing, Databases, and Performance

Unlock this course with a free trial

Join today to access over 25,200 courses taught by industry experts.

Table creation and adding data

Table creation and adding data

Now that the database is created, let's begin with the table creation. To create a new table in SQLite, we use the create table query. But how do we execute the queries while using a driver? Well, the better SQLite 3 offers various methods to execute queries. So to execute the create table query, we use the prepare method to compile and stage the query. After the prepare method, we call the run method to execute the command. Let's practically try it. I'll say db.prepare and write the query create table if not exists products, the field name id, integer, primary key and autoincrement, name, text, price real and image text. Now we will call the run method to execute this command show.run. Let's save and run the code. Let's go to the products.sqlite and there we have the products table created. Now just like this, let's also add some data into the table. So we use the insertInto query with the prepareAndRun method. I'll say db.prepare, insertInto products, name, price, image, values…

Contents