[Risolto] app android salvare dati in DB SQLLite
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
[Risolto] app android salvare dati in DB SQLLite
Buongiorno amici
sto facendo la mia seconda app android e ho un piccolo intoppo....la mia app deve essere in grado di trovare tutti gli hotspot vicino a me e salvarne le info in un database sqlite... il mio problema è che sono arrivato a scansionare le reti e mi prende le info ma come faccio a salvarle in un db??
ecco qui i dati che dovrebbe salvare
https://www.dropbox.com/s/uq9d4nl08firt ... 2%2045.jpg
sto facendo la mia seconda app android e ho un piccolo intoppo....la mia app deve essere in grado di trovare tutti gli hotspot vicino a me e salvarne le info in un database sqlite... il mio problema è che sono arrivato a scansionare le reti e mi prende le info ma come faccio a salvarle in un db??
ecco qui i dati che dovrebbe salvare
https://www.dropbox.com/s/uq9d4nl08firt ... 2%2045.jpg
Ultima modifica di Chry1991 il venerdì 20 giugno 2014, 12:06, modificato 1 volta in totale.
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
sono riuscito ad ottenere le stringhe che mi servono, xò ho un problemino
ho creato la classe database solo che non riesco a inserire i dati che mi servono nel db,
vi posto la classe database:
nella classe scan faccio questa chiamata ad un metodo del db
solo che mi da una nullpointerexception e non capisco xkè....i valori me li prende...se stampo in console le stringhe mi stampa correttamente ad esempio rete name stampa alice-123456
ho creato la classe database solo che non riesco a inserire i dati che mi servono nel db,
vi posto la classe database:
Codice: Seleziona tutto
import java.text.MessageFormat;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper
{
private static final String DATABASE_NAME = "hotspot.db";
private static final int SCHEMA_VERSION = 1;
SQLiteDatabase db;
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
createTableHotspot(db);
}
private void createTableHotspot(SQLiteDatabase db)
{
String sql = "CREATE TABLE {0} ({1} INTEGER PRIMARY KEY AUTOINCREMENT, {2} TEXT NOT NULL,{3} TEXT NOT NULL, {4} INTEGER NOT NULL);";
db.execSQL(MessageFormat.format(sql, HotSpotTable.TABLE_NAME, HotSpotTable._ID, HotSpotTable.NOME_RETE, HotSpotTable.SICUREZZA, HotSpotTable.COPERTURA));
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (oldVersion < 2)
{
createTableHotspot(db);
}
}
public void insertHotspot(SQLiteDatabase db,int id, String name, String security, int coperture)
{
ContentValues v = new ContentValues();
v.put(HotSpotTable.id, id);
v.put(HotSpotTable.NOME_RETE, name);
v.put(HotSpotTable.SICUREZZA, security);
v.put(HotSpotTable.COPERTURA, coperture);
db.insert(HotSpotTable.TABLE_NAME, null, v);
}
public Cursor getAllHotspots()
{
String query = "select nome from Hotspots ";
return getReadableDatabase().rawQuery(query, null);
}
}nella classe scan faccio questa chiamata ad un metodo del db
Codice: Seleziona tutto
int id = 0;
String Rete_name=wifiScanList.get(i).SSID.toString();
String Security =wifiScanList.get(i).capabilities.toString();
int coperture = wifiScanList.get(i).frequency;
database.insertHotspot(db, id, Rete_name, Security, coperture);
- difesaparcosempione
- Rampante Reduce

- Messaggi: 6031
- Iscrizione: giovedì 27 luglio 2006, 19:06
- Località: Torino
- Contatti:
Re: app android salvare dati in DB SQLLite
Hai dichiarato variabili ma immagino non lo "spazio" dedicato ...
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
cioè??
a me l'errore che mi da è questo:
java.lang.RuntimeException: Error receiving broadcast Intent
{ act=android.net.wifi.SCAN_RESULTS flg=0x8000010 }
in com.example.wifihotspotfinder.WifiActivity$WifiScanReceiver@419eb9a8
a me l'errore che mi da è questo:
java.lang.RuntimeException: Error receiving broadcast Intent
{ act=android.net.wifi.SCAN_RESULTS flg=0x8000010 }
in com.example.wifihotspotfinder.WifiActivity$WifiScanReceiver@419eb9a8
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
può essere che mentre sta scansionando le reti io contemporaneamente cerco di scrivere nel db??
é l'unica cosa che posso pensare, xkè se io tolgo quella riga di codice l'errore non mi si presenta.
é l'unica cosa che posso pensare, xkè se io tolgo quella riga di codice l'errore non mi si presenta.
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
sono riuscito a creare il database solo che ho questo problema ma non riesco a venirne a capo...
mi da questo errore
la mia classe database è questa:
e la classe ap è questa:
non ho capito cosa sbaglio davvero ci sto diventando matto con sto maledetto sqlite
mi da questo errore
Codice: Seleziona tutto
06-18 19:57:13.284: E/SQLiteLog(10854): (1) table hotspot has no column named coperture
06-18 19:57:13.288: E/SQLiteDatabase(10854): Error inserting security=null coperture=0 name=null
06-18 19:57:13.288: E/SQLiteDatabase(10854): android.database.sqlite.SQLiteException: table hotspot has no column named coperture (code 1): , while compiling: INSERT INTO hotspot(security,coperture,name) VALUES (?,?,?)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:886)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:497)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at com.example.wifihotspotfinder.MySQLiteHelper.addAccessPoint(MySQLiteHelper.java:75)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at com.example.wifihotspotfinder.SearchHotspotsActivity.onCreate(SearchHotspotsActivity.java:47)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.Activity.performCreate(Activity.java:5122)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.ActivityThread.access$600(ActivityThread.java:162)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.os.Handler.dispatchMessage(Handler.java:107)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.os.Looper.loop(Looper.java:194)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at android.app.ActivityThread.main(ActivityThread.java:5371)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at java.lang.reflect.Method.invoke(Method.java:525)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
06-18 19:57:13.288: E/SQLiteDatabase(10854): at dalvik.system.NativeStart.main(Native Method)
la mia classe database è questa:
Codice: Seleziona tutto
import java.util.LinkedList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class MySQLiteHelper extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "HotspotDB";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SQL statement to create book table
String CREATE_HOTSPOT_TABLE = "CREATE TABLE hotspot ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, "+
"security TEXT, " +
"coperture INTEGER)";
// create books table
db.execSQL(CREATE_HOTSPOT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older books table if existed
db.execSQL("DROP TABLE IF EXISTS hotspot");
// create fresh books table
this.onCreate(db);
}
//---------------------------------------------------------------------
/**
* CRUD operations (create "add", read "get", update, delete) book + get all books + delete all books
*/
// Accesspoint table name
private static final String TABLE_ACCESSPOINT = "hotspot";
// Accesspoint Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_SSID = "name";
private static final String KEY_SECURITY = "security";
private static final String KEY_COPERTURE = "coperture";
private static final String[] COLUMNS = {KEY_ID,KEY_SSID,KEY_SECURITY,KEY_COPERTURE};
public void addAccessPoint(AccessPoint ap){
Log.d("accesspoint", ap.toString());
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues values = new ContentValues();
values.put(KEY_SSID, ap.getName()); // get name rete
values.put(KEY_SECURITY, ap.getSecurity()); //get security
values.put(KEY_COPERTURE, ap.getCoperture());// get coperture
// 3. insert
db.insert(TABLE_ACCESSPOINT, // table
null, //nullColumnHack
values); // key/value -> keys = column names/ values = column values
// 4. close
db.close();
}
public AccessPoint getSSID(int id){
// 1. get reference to readable DB
SQLiteDatabase db = this.getReadableDatabase();
// 2. build query
Cursor cursor =
db.query(TABLE_ACCESSPOINT, // a. table
COLUMNS, // b. column names
" id = ?", // c. selections
new String[] { String.valueOf(id) }, // d. selections args
null, // e. group by
null, // f. having
null, // g. order by
null); // h. limit
// 3. if we got results get the first one
if (cursor != null)
cursor.moveToFirst();
// 4. build book object
AccessPoint ap = new AccessPoint();
ap.setId(Integer.parseInt(cursor.getString(0)));
ap.setName(cursor.getString(1));
ap.setSecurity(cursor.getString(2));
ap.setCoperture(Integer.parseInt(cursor.getString(3)));
Log.d("getAP("+id+")", ap.toString());
// 5. return book
return ap;
}
//Get All Books
public List<AccessPoint> getAllAP() {
List<AccessPoint> apoints = new LinkedList<AccessPoint>();
// 1. build the query
String query = "SELECT * FROM " + TABLE_ACCESSPOINT;
// 2. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
// 3. go over each row, build book and add it to list
AccessPoint ap = null;
if (cursor.moveToFirst()) {
do {
ap = new AccessPoint();
ap.setId(Integer.parseInt(cursor.getString(0)));
ap.setName(cursor.getString(1));
ap.setSecurity(cursor.getString(2));
ap.setCoperture(Integer.parseInt(cursor.getString(3)));
// Add book to books
apoints.add(ap);
} while (cursor.moveToNext());
}
Log.d("getAllAPoints()", ap.toString());
// return books
return apoints;
}
}e la classe ap è questa:
Codice: Seleziona tutto
public class AccessPoint {
private int id;
private String name;
private String security;
private int coperture;
public AccessPoint(){
}
public AccessPoint(String name, String security, int coperture){
super();
this.name = name;
this.security = security;
this.coperture = coperture;
}
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSecurity(){
return security;
}
public void setSecurity(String security){
this.security = security;
}
public int getCoperture(){
return coperture;
}
public void setCoperture(int coperture){
this.coperture=coperture;
}
@Override
public String toString() {
return "AccessPoint [id=" + id + ", nomeRete=" + name + ", Sicurezza=" + security
+ "]";
}
}
non ho capito cosa sbaglio davvero ci sto diventando matto con sto maledetto sqlite
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
risolto da solo...si puo chiudere
- jackynet92
- Moderatore Globale

- Messaggi: 13413
- Iscrizione: sabato 3 settembre 2011, 1:41
- Desktop: Mate
- Distribuzione: Ubuntu 16.04 64bit
- Sesso: Maschile
- Località: Torino
Re: app android salvare dati in DB SQLLite
Se ritieni risolto il problema, modifica il titolo del primo post aggiungendo all'inizio [Risolto].
Se vuoi puoi installare questo script che ti aggiunge un pulsante che ti permette di mettere [Risolto] con un solo click.
Le discussioni non si chiudono, salvo casi particolari e spiega come hai risolto.
Alla prossima
Se vuoi puoi installare questo script che ti aggiunge un pulsante che ti permette di mettere [Risolto] con un solo click.
Le discussioni non si chiudono, salvo casi particolari e spiega come hai risolto.
Alla prossima
I limiti esistono solo perché noi possiamo superarli.
-
Chry1991
- Scoppiettante Seguace

- Messaggi: 461
- Iscrizione: giovedì 23 maggio 2013, 20:20
- Desktop: Linux Mint 16.04
- Distribuzione: Linux Mint 16.04 Xubuntu 16.04
- Sesso: Maschile
Re: app android salvare dati in DB SQLLite
jackynet92 [url=http://forum.ubuntu-it.org/viewtopic.php?p=4602612#p4602612][img]http://forum.ubuntu-it.org/images/icons/icona-cita.gif[/img][/url] ha scritto:Se ritieni risolto il problema, modifica il titolo del primo post aggiungendo all'inizio [Risolto].
Se vuoi puoi installare questo script che ti aggiunge un pulsante che ti permette di mettere [Risolto] con un solo click.
Le discussioni non si chiudono, salvo casi particolari e spiega come hai risolto.
Alla prossima
ho risposto spostando il comando db.inserthotspot
nella sotto classe di wifireciever tutto qua...ora mi prende i parametri giusti e non rimangono null...
Chi c’è in linea
Visualizzano questa sezione: 0 utenti iscritti e 4 ospiti