· 6 years ago · Jul 02, 2019, 07:28 PM
1import java.sql.SQLException;
2import java.sql.Statement;
3
4public class CreateDatabaseSQLite {
5
6 private final ConnectionSQLite connectSQLite;
7
8 public CreateDatabaseSQLite(ConnectionSQLite pConnectSQLite) {
9 this.connectSQLite = pConnectSQLite;
10 }
11
12 public void createTablePeople() {
13
14 String slq = "CREATE TABLE IF NOT EXISTS tb_people"
15 + "("
16 + "id integer PRIMARY KEY,"
17 + "nome text NOT NULL,"
18 + "idade integer"
19 + ");";
20
21 // executando o sql de criar tabelas
22 boolean connect = false;
23
24 try {
25
26 connect = this.connectSQLite.connect();
27
28 Statement stmt = this.connectSQLite.createStatement();
29
30 stmt.execute(slq);
31
32 System.out.println("Tabela pessoa criada!");
33
34 } catch (SQLException e) {
35 // mensagem de erro na criação da tabela
36 System.err.println("Erro a o criar tabela: " + e);
37 } finally {
38 if (connect) {
39 this.connectSQLite.disconnect();
40 }
41 }
42 }