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

Filter Text in ExpandableListView using BaseExpandableListAdapter
changing Message for ProgressDialog android
use SwipeView for a layout rather than images
Looking for sample application which demonstrates drawables for different screens [closed]
Is there a way to dynamically change the preference screen font color?
Control a edittext from other XML file
How do we get notification when app is installed on android
Text not displayed in a todolist application
Closing multiple Dialogs at once
Tried everything — Conversion to Dalvik format failed with error 1
ISO for Android Google APIs for VirtualBox
How can I disassemble an Android dex file within Eclipse?
onAccessibilityEvent(AccessibilityEvent event) not intercepting notification
With Android APIs, can I simulate/create an incoming call?
LinkedIn RequestToken FAil Android Error “”Communication with the service provider failed: https://api.linkedin.com/uas/oauth/requestToken“”
how to ignore XML errors when parsing faulty XML with Sax (on Android)

Categories

HOME
gdb
string
obfuscation
devise
owl-carousel
ms-access-2016
static-site
xbee
vxml
bookmarks
data-visualization
teechart
startup
java.util.logging
gkturnbasedmatch
plink
selectize.js
docx
package-managers
screen-readers
feathersjs
foreign-keys
jmp
sigma.js
selinux
spring-async
question2answer
xunit
sinch
errbot
sqlpackage
event-flow
recurring-events
utorrent
testbed
git-squash
xajax
cd
demo
chef-solo
monkey-testing
snapchat
chai-as-promised
podscms
arq
seq
rocks
redbeard-ios
greatest-n-per-group
dimension
cfchart
createprocess
nupic
nio2
rasterize
highland.js
spring-retry
vhosts
jdi
ons-api
video-player
sony-lifelog-api
typemock
airbrake
j-security-check
procedural-programming
cross-join
webmatrix-3
administration
big-ip
appserver
paw
insight.database
mdichild
angular-carousel
lambda-architecture
dct
paste
radiant
maven-scm
menubar
http-patch
printdialog
script#
observium
workitem
scmmanager
css-reset
diem
mysql-error-2013
http-status-code-410
booksleeve
luabind
catransform3d
cac
timertask
office-2007
spread
purepdf
leader

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