Connecting my app to a SQLite database - how to?












1















I am creating a login app, where a user has an username and a password. I want to save sign up data in my database, but when I want to get access to the elements from database, no one seems to be saved ( it has no user saved in db). I suppose the error comes from db implementation. Any idea?



public class DataBaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "user_table";
public static final String COL1 = "ID";
public static final String COL2 = "user_username";
public static final String COL3 = "user_password";

public DataBaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "( " + COL2 + " TEXT," +
COL3 + "TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void add(String name, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, password);
db.insert(TABLE_NAME, null, contentValues);
}

public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT *FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}


LOGIN Activity:



public void loginSeach(View view) {
EditText password_editText = (EditText) findViewById(R.id.editText_password);
EditText username_editText = (EditText) findViewById(R.id.editText_username);
String currUserName = username_editText.getText().toString();
String currPassWord = password_editText.getText().toString();
DataBaseHelper db = new DataBaseHelper(this);
Cursor data = db.getData();
int Number = 0;
while (data.moveToNext()) {
Number ++;
String userName = data.getString(1);
String passWord = data.getString(2);
}

Toast.makeText(this, Integer.toString(Number), Toast.LENGTH_SHORT).show();

}


Sign up activity:



public void createAccount(View view) {
EditText usernameEditText = (EditText) findViewById(R.id.editText_createUsername);
EditText passwordEditText = (EditText) findViewById(R.id.editText_createPassword);
String userName = usernameEditText.getText().toString();
String passWord = passwordEditText.getText().toString();

dataBaseHelper.add(userName, passWord);

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

}









share|improve this question

























  • How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

    – Mike M.
    Nov 25 '18 at 17:42











  • @MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

    – Blasanka
    Nov 25 '18 at 17:43











  • And also how do you displaying the values in getData() table

    – Blasanka
    Nov 25 '18 at 17:43











  • @MikeM. I placed a space there, but the same result

    – Mihai Myhai
    Nov 25 '18 at 17:48
















1















I am creating a login app, where a user has an username and a password. I want to save sign up data in my database, but when I want to get access to the elements from database, no one seems to be saved ( it has no user saved in db). I suppose the error comes from db implementation. Any idea?



public class DataBaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "user_table";
public static final String COL1 = "ID";
public static final String COL2 = "user_username";
public static final String COL3 = "user_password";

public DataBaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "( " + COL2 + " TEXT," +
COL3 + "TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void add(String name, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, password);
db.insert(TABLE_NAME, null, contentValues);
}

public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT *FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}


LOGIN Activity:



public void loginSeach(View view) {
EditText password_editText = (EditText) findViewById(R.id.editText_password);
EditText username_editText = (EditText) findViewById(R.id.editText_username);
String currUserName = username_editText.getText().toString();
String currPassWord = password_editText.getText().toString();
DataBaseHelper db = new DataBaseHelper(this);
Cursor data = db.getData();
int Number = 0;
while (data.moveToNext()) {
Number ++;
String userName = data.getString(1);
String passWord = data.getString(2);
}

Toast.makeText(this, Integer.toString(Number), Toast.LENGTH_SHORT).show();

}


Sign up activity:



public void createAccount(View view) {
EditText usernameEditText = (EditText) findViewById(R.id.editText_createUsername);
EditText passwordEditText = (EditText) findViewById(R.id.editText_createPassword);
String userName = usernameEditText.getText().toString();
String passWord = passwordEditText.getText().toString();

dataBaseHelper.add(userName, passWord);

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

}









share|improve this question

























  • How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

    – Mike M.
    Nov 25 '18 at 17:42











  • @MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

    – Blasanka
    Nov 25 '18 at 17:43











  • And also how do you displaying the values in getData() table

    – Blasanka
    Nov 25 '18 at 17:43











  • @MikeM. I placed a space there, but the same result

    – Mihai Myhai
    Nov 25 '18 at 17:48














1












1








1








I am creating a login app, where a user has an username and a password. I want to save sign up data in my database, but when I want to get access to the elements from database, no one seems to be saved ( it has no user saved in db). I suppose the error comes from db implementation. Any idea?



public class DataBaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "user_table";
public static final String COL1 = "ID";
public static final String COL2 = "user_username";
public static final String COL3 = "user_password";

public DataBaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "( " + COL2 + " TEXT," +
COL3 + "TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void add(String name, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, password);
db.insert(TABLE_NAME, null, contentValues);
}

public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT *FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}


LOGIN Activity:



public void loginSeach(View view) {
EditText password_editText = (EditText) findViewById(R.id.editText_password);
EditText username_editText = (EditText) findViewById(R.id.editText_username);
String currUserName = username_editText.getText().toString();
String currPassWord = password_editText.getText().toString();
DataBaseHelper db = new DataBaseHelper(this);
Cursor data = db.getData();
int Number = 0;
while (data.moveToNext()) {
Number ++;
String userName = data.getString(1);
String passWord = data.getString(2);
}

Toast.makeText(this, Integer.toString(Number), Toast.LENGTH_SHORT).show();

}


Sign up activity:



public void createAccount(View view) {
EditText usernameEditText = (EditText) findViewById(R.id.editText_createUsername);
EditText passwordEditText = (EditText) findViewById(R.id.editText_createPassword);
String userName = usernameEditText.getText().toString();
String passWord = passwordEditText.getText().toString();

dataBaseHelper.add(userName, passWord);

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

}









share|improve this question
















I am creating a login app, where a user has an username and a password. I want to save sign up data in my database, but when I want to get access to the elements from database, no one seems to be saved ( it has no user saved in db). I suppose the error comes from db implementation. Any idea?



public class DataBaseHelper extends SQLiteOpenHelper {

public static final String TABLE_NAME = "user_table";
public static final String COL1 = "ID";
public static final String COL2 = "user_username";
public static final String COL3 = "user_password";

public DataBaseHelper(Context context) {
super(context, TABLE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + "( " + COL2 + " TEXT," +
COL3 + "TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public void add(String name, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL2, name);
contentValues.put(COL3, password);
db.insert(TABLE_NAME, null, contentValues);
}

public Cursor getData() {
SQLiteDatabase db = this.getWritableDatabase();
String query = "SELECT *FROM " + TABLE_NAME;
Cursor data = db.rawQuery(query, null);
return data;
}


LOGIN Activity:



public void loginSeach(View view) {
EditText password_editText = (EditText) findViewById(R.id.editText_password);
EditText username_editText = (EditText) findViewById(R.id.editText_username);
String currUserName = username_editText.getText().toString();
String currPassWord = password_editText.getText().toString();
DataBaseHelper db = new DataBaseHelper(this);
Cursor data = db.getData();
int Number = 0;
while (data.moveToNext()) {
Number ++;
String userName = data.getString(1);
String passWord = data.getString(2);
}

Toast.makeText(this, Integer.toString(Number), Toast.LENGTH_SHORT).show();

}


Sign up activity:



public void createAccount(View view) {
EditText usernameEditText = (EditText) findViewById(R.id.editText_createUsername);
EditText passwordEditText = (EditText) findViewById(R.id.editText_createPassword);
String userName = usernameEditText.getText().toString();
String passWord = passwordEditText.getText().toString();

dataBaseHelper.add(userName, passWord);

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

}






java android sqlite






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 18:30









Fantômas

32.8k156490




32.8k156490










asked Nov 25 '18 at 17:38









Mihai MyhaiMihai Myhai

166




166













  • How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

    – Mike M.
    Nov 25 '18 at 17:42











  • @MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

    – Blasanka
    Nov 25 '18 at 17:43











  • And also how do you displaying the values in getData() table

    – Blasanka
    Nov 25 '18 at 17:43











  • @MikeM. I placed a space there, but the same result

    – Mihai Myhai
    Nov 25 '18 at 17:48



















  • How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

    – Mike M.
    Nov 25 '18 at 17:42











  • @MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

    – Blasanka
    Nov 25 '18 at 17:43











  • And also how do you displaying the values in getData() table

    – Blasanka
    Nov 25 '18 at 17:43











  • @MikeM. I placed a space there, but the same result

    – Mihai Myhai
    Nov 25 '18 at 17:48

















How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

– Mike M.
Nov 25 '18 at 17:42





How are you determining that there are no users in the database? Your SELECT statement has a typo – "SELECT *FROM " + TABLE_NAME. You're missing a space there, after the asterisk.

– Mike M.
Nov 25 '18 at 17:42













@MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

– Blasanka
Nov 25 '18 at 17:43





@MikeM. also what do you mean by I suppose the error comes from db implementation please check the logcat first when you are inserting values

– Blasanka
Nov 25 '18 at 17:43













And also how do you displaying the values in getData() table

– Blasanka
Nov 25 '18 at 17:43





And also how do you displaying the values in getData() table

– Blasanka
Nov 25 '18 at 17:43













@MikeM. I placed a space there, but the same result

– Mihai Myhai
Nov 25 '18 at 17:48





@MikeM. I placed a space there, but the same result

– Mihai Myhai
Nov 25 '18 at 17:48












1 Answer
1






active

oldest

votes


















1














This statement in onCreate():



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";


created a table without an ID column.

Uninstall the app from the emulator/device, change your code to this:



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";


and rerun the app.






share|improve this answer


























  • And how to insert the ID column in createTable?

    – Mihai Myhai
    Nov 25 '18 at 17:49











  • Give me a few mins and I will post the code.

    – forpas
    Nov 25 '18 at 17:51











  • See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

    – forpas
    Nov 25 '18 at 17:55











  • A space missing before the last TEXT

    – Lluis Felisart
    Nov 25 '18 at 18:00











  • Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

    – Mihai Myhai
    Nov 25 '18 at 18:00











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470130%2fconnecting-my-app-to-a-sqlite-database-how-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This statement in onCreate():



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";


created a table without an ID column.

Uninstall the app from the emulator/device, change your code to this:



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";


and rerun the app.






share|improve this answer


























  • And how to insert the ID column in createTable?

    – Mihai Myhai
    Nov 25 '18 at 17:49











  • Give me a few mins and I will post the code.

    – forpas
    Nov 25 '18 at 17:51











  • See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

    – forpas
    Nov 25 '18 at 17:55











  • A space missing before the last TEXT

    – Lluis Felisart
    Nov 25 '18 at 18:00











  • Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

    – Mihai Myhai
    Nov 25 '18 at 18:00
















1














This statement in onCreate():



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";


created a table without an ID column.

Uninstall the app from the emulator/device, change your code to this:



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";


and rerun the app.






share|improve this answer


























  • And how to insert the ID column in createTable?

    – Mihai Myhai
    Nov 25 '18 at 17:49











  • Give me a few mins and I will post the code.

    – forpas
    Nov 25 '18 at 17:51











  • See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

    – forpas
    Nov 25 '18 at 17:55











  • A space missing before the last TEXT

    – Lluis Felisart
    Nov 25 '18 at 18:00











  • Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

    – Mihai Myhai
    Nov 25 '18 at 18:00














1












1








1







This statement in onCreate():



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";


created a table without an ID column.

Uninstall the app from the emulator/device, change your code to this:



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";


and rerun the app.






share|improve this answer















This statement in onCreate():



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL2 + " TEXT," + COL3 + "TEXT)";


created a table without an ID column.

Uninstall the app from the emulator/device, change your code to this:



String createTable = "CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL2 + " TEXT," + COL3 + " TEXT)";


and rerun the app.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 18:00

























answered Nov 25 '18 at 17:48









forpasforpas

17.3k3628




17.3k3628













  • And how to insert the ID column in createTable?

    – Mihai Myhai
    Nov 25 '18 at 17:49











  • Give me a few mins and I will post the code.

    – forpas
    Nov 25 '18 at 17:51











  • See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

    – forpas
    Nov 25 '18 at 17:55











  • A space missing before the last TEXT

    – Lluis Felisart
    Nov 25 '18 at 18:00











  • Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

    – Mihai Myhai
    Nov 25 '18 at 18:00



















  • And how to insert the ID column in createTable?

    – Mihai Myhai
    Nov 25 '18 at 17:49











  • Give me a few mins and I will post the code.

    – forpas
    Nov 25 '18 at 17:51











  • See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

    – forpas
    Nov 25 '18 at 17:55











  • A space missing before the last TEXT

    – Lluis Felisart
    Nov 25 '18 at 18:00











  • Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

    – Mihai Myhai
    Nov 25 '18 at 18:00

















And how to insert the ID column in createTable?

– Mihai Myhai
Nov 25 '18 at 17:49





And how to insert the ID column in createTable?

– Mihai Myhai
Nov 25 '18 at 17:49













Give me a few mins and I will post the code.

– forpas
Nov 25 '18 at 17:51





Give me a few mins and I will post the code.

– forpas
Nov 25 '18 at 17:51













See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

– forpas
Nov 25 '18 at 17:55





See my edited code. Follow the instructions before changing the code. Of course the table will be empty.

– forpas
Nov 25 '18 at 17:55













A space missing before the last TEXT

– Lluis Felisart
Nov 25 '18 at 18:00





A space missing before the last TEXT

– Lluis Felisart
Nov 25 '18 at 18:00













Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

– Mihai Myhai
Nov 25 '18 at 18:00





Well, I did this ( changed the code and unistalled the app), but still the same result. I place my sign up code and login code(in the login I just check the number of registration)

– Mihai Myhai
Nov 25 '18 at 18:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53470130%2fconnecting-my-app-to-a-sqlite-database-how-to%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen