Ací es mostren les diferències entre la revisió seleccionada i la versió actual de la pàgina.
Següent revisió | Revisió prèvia | ||
java_sqlite [2022/11/23 18:11] albert_palacios_jimenez creat |
java_sqlite [2022/11/23 18:18] albert_palacios_jimenez |
||
---|---|---|---|
Línia 1: | Línia 1: | ||
- | ====== SQLite | + | ====== SQLite |
{{tag> #FpInfor #Dam #DamMp06 #DamMp06Uf2 # | {{tag> #FpInfor #Dam #DamMp06 #DamMp06Uf2 # | ||
Línia 7: | Línia 7: | ||
---- | ---- | ||
- | ==== Exemple | + | ==== Exemples |
{{ : | {{ : | ||
Línia 261: | Línia 261: | ||
} catch (SQLException e) { e.printStackTrace(); | } catch (SQLException e) { e.printStackTrace(); | ||
return rs; | return rs; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Exemple, iniciar dades a SQLite ==== | ||
+ | |||
+ | Aquest exemple mostra com iniciar a SQLite (fa ús de UtilsSQLite.java anterior) | ||
+ | |||
+ | Main.java | ||
+ | |||
+ | <code java> | ||
+ | import java.io.File; | ||
+ | import java.sql.Connection; | ||
+ | import java.sql.ResultSet; | ||
+ | import java.sql.ResultSetMetaData; | ||
+ | import java.sql.SQLException; | ||
+ | import java.util.ArrayList; | ||
+ | |||
+ | /* | ||
+ | * Aquest exemple mostra com fer una | ||
+ | * connexió a SQLite amb Java | ||
+ | | ||
+ | * A la primera crida, crea l' | ||
+ | * de base de dades hi posa dades, | ||
+ | * després les modifica | ||
+ | | ||
+ | * A les següent crides ja estan | ||
+ | * originalment modificades | ||
+ | * (tot i que les sobreescriu cada vegada) | ||
+ | */ | ||
+ | |||
+ | public class Main { | ||
+ | |||
+ | public static void main(String[] args) throws SQLException { | ||
+ | String basePath = System.getProperty(" | ||
+ | String filePath = basePath + " | ||
+ | ResultSet rs = null; | ||
+ | |||
+ | // Si no hi ha l' | ||
+ | File fDatabase = new File(filePath); | ||
+ | if (!fDatabase.exists()) { initDatabase(filePath); | ||
+ | |||
+ | // Connectar (crea la BBDD si no existeix) | ||
+ | Connection conn = UtilsSQLite.connect(filePath); | ||
+ | |||
+ | // Llistar les taules | ||
+ | ArrayList< | ||
+ | System.out.println(taules); | ||
+ | |||
+ | // Demanar informació de la taula | ||
+ | rs = UtilsSQLite.querySelect(conn, | ||
+ | ResultSetMetaData rsmd = rs.getMetaData(); | ||
+ | System.out.println(" | ||
+ | for (int cnt = 1; cnt < rsmd.getColumnCount(); | ||
+ | // Les columnes començen a 1, no hi ha columna 0! | ||
+ | String label = rsmd.getColumnLabel(cnt); | ||
+ | String name = rsmd.getColumnName(cnt); | ||
+ | int type = rsmd.getColumnType(cnt); | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | // SELECT a la base de dades | ||
+ | rs = UtilsSQLite.querySelect(conn, | ||
+ | System.out.println(" | ||
+ | while (rs.next()) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | // Actualitzar una fila | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | |||
+ | // Esborrar una fila | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | |||
+ | // SELECT a la base de dades | ||
+ | rs = UtilsSQLite.querySelect(conn, | ||
+ | System.out.println(" | ||
+ | while (rs.next()) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | // Desconnectar | ||
+ | UtilsSQLite.disconnect(conn); | ||
+ | } | ||
+ | |||
+ | static void initDatabase (String filePath) { | ||
+ | // Connectar (crea la BBDD si no existeix) | ||
+ | Connection conn = UtilsSQLite.connect(filePath); | ||
+ | |||
+ | // Esborrar la taula (per si existeix) | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | |||
+ | // Crear una nova taula | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | + " id integer PRIMARY KEY AUTOINCREMENT," | ||
+ | + " name text NOT NULL);" | ||
+ | |||
+ | // Afegir elements a una taula | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | UtilsSQLite.queryUpdate(conn, | ||
+ | |||
+ | // Desconnectar | ||
+ | UtilsSQLite.disconnect(conn); | ||
} | } | ||
} | } | ||
</ | </ |