android


SQLite doesn't insert values


I don't know why, but when I log in into my app it doesn't create the values in the SQLite database
this is my code:
public class SQLiteHandler extends SQLiteOpenHelper {
private static final String TAG = SQLiteHandler.class.getSimpleName();
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
// Login table name
private static final String TABLE_USERS = "users";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "nome";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_DATA_NASCIMENTO = "data_nascimento";
private static final String KEY_DATA_CRIADO = "user_datacriado";
public SQLiteHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
#Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_USERS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE," + KEY_UID + " TEXT,"
+ KEY_DATA_CRIADO + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
Log.d(TAG, "Database tables created");
}
// Upgrading database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
// Create tables again
onCreate(db);
}
/**
* Storing user details in database
* */
public void addUser(String nome, String email, String uid, String data_nascimento, String user_datacriado) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, nome); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_DATA_NASCIMENTO, data_nascimento); // Created At
values.put(KEY_DATA_CRIADO, user_datacriado); // Created At
// Inserting Row
long id = db.insert(TABLE_USERS, null, values);
db.close(); // Closing database connection
Log.d(TAG, "New user inserted into sqlite: " + id);
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
String selectQuery = "SELECT * FROM " + TABLE_USERS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if (cursor.getCount() > 0) {
user.put("nome", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
Log.d(TAG, "Fetching user from Sqlite: " + user.toString());
return user;
}
/**
* Re crate database Delete all tables and create them again
* */
public void deleteUsers() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_USERS, null, null);
db.close();
Log.d(TAG, "Deleted all user info from sqlite");
}
}
This is in the login fragment:
#Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("nome");
String email = user.getString("email");
String data_nascimento = user.getString("data_nascimento");
String user_datacriado = user.getString("user_datacriado");
// Inserting row in users table
db.addUser(name, email, uid, data_nascimento, user_datacriado);
// Launch main activity
//Intent intent = new Intent(LoginActivity.this,
//MainActivity.class);
//startActivity(intent);
//finish();
// Launch login activity
Intent intent = new Intent(
getActivity(),
MainActivity.class);
startActivity(intent);
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
...
While debugging, the adduser(...) method:
long id = db.insert(TABLE_USERS, null, values);
id is always -1 and I don't know why.
One problem is that your CREATE TABLE is missing the SQL for the data_nascimento column you're trying to insert values into.
After adding it, you can uninstall your app to recreate the database.
Try to add to Autoincrement to Id PRIMARY KEY column.

Related Links

android from FragmentPagerAdapter return the current fragment position to calling activity
Intercept button on bluetooth headset
How to handle large images in Android activity
Fragment still visible after popBackStack
Android language parsing difficulties
Change EditText Cursor color through styles.xml
Runtime exception when creating a new PdfRenderer
Get physically mounted external storage directory (not internal storage) regardless of android version
SearchView in Collapsing toolbar gets hided when scrolled up
Spinner automatically change the style
how to send assets like an html page in response of AsyncHttpServer in Android
getResources in a regular Class which IntentService and Fragment call them functions
Is there a way to communicate reliably via Bluetooth?
Android custom keyboard: change candidate height
GCM push not working with parse
I'm trying to load data from mysql in textview

Categories

HOME
embedded
search
phpword
porting
localization
facebook-oauth
angular2-material
case
sitecore
parameters
emscripten
nested-if
x264
distribution
text-mining
flatpak
symbol
libusb
gruntfile
azure-powershell
glyphicons
fileserver
forgot-password
cfml
sigma.js
symbols
googlebot
backpack-for-laravel
ember-cli
joptionpane
epsilon
google-api-client
surveymonkey
fragment-backstack
outliers
linked-data
gp
sqsh
testcomplete
google-cast
gitlab-api
event-flow
complex-numbers
catch-unit-test
code-snippets
pop3
pdw-file-browser
browser-extension
roblox
magento-1.9.2.4
parse-tree
openproject
sql-server-ce
railstutorial.org
health-monitoring
placeholder
contract
pom.xml
flask-restful
getchar
ntvs
gitlist
serde
dtsearch
convex-optimization
qtranslate
subnet
skin
jtree
android-instrumentation
pre-build-event
jtwig
riot
qbxml
toast
qt5.2
poppler
keyup
public-html
document-repository
easyrtc
human-computer-interface
create.js
reflector
memset
maven-scm
git-repo
jeromq
workitem
device-width
pdfviewer
sentestingkit
wcf-configuration
dajaxice
cgimage
agility.js
appledoc
nhprof
photolibrary
presentation-layer
maven-1
lxr
strtod
visualj#
scrubyt
wcf-proxy

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App