How to save data in the database

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

How to save data in the database

Postby gurnoorinder » Thu Mar 24, 2011 10:18 pm

hello,

Can anyone help me by explaining how i can save data in the database, which I ask the user to enter. Say there are two fields. One is the user name and the other is the user age. And i want to save this data in a database table. it will have an AUTO INCREMENT id as its PRIMARY KEY.

thanks
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Top

Re: How to save data in the database

Postby DMacATTACK » Fri Mar 25, 2011 1:38 am

[Note this code is untested, but outlines how to do what you want and this example assumes you have a basic knowledge of how to use buttons, EditTexts, etc ]

To "save" data into the database you are actually performing an "INSERT" query.
Lets say for this example you have already created the database and table using a query such as:

CREATE TABLE IF NOT EXISTS tblPeople (intID INTEGER PRIMARY KEY, strName VARCHAR(30), intAge INTEGER);

Which can be implemented in Java like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.                // ------ create Database Object -------
  3.               private SQLiteDatabase dbPeople;
  4.  
  5.                 // ----- create the Database -----
  6.               dbPeople= this.openOrCreateDatabase("dbPeople.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
  7.                 dbPeople.setLocale(Locale.getDefault());
  8.                 dbPeople.setLockingEnabled(true);
  9.                 dbPeople.setVersion(1);
  10.                
  11.                 // ----- create the table -----
  12.                 String strCreateDataTable = "CREATE TABLE IF NOT EXISTS tblPeople (intID INTEGER PRIMARY KEY, strName VARCHAR(30), intAge INTEGER);";  
  13.                
  14.                 try{           
  15.                         dbPeople.execSQL(strCreateDataTable);
  16.                 }
  17.                 catch(Exception ex)
  18.                 {
  19.                 Log.d("TEST", "Exception = >" + ex.toString());
  20.                 }
  21.                
  22.                 dbPeople.close();      
  23.         }
  24.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Okay lets say there are two EditTextFields: tbxName and tbxAge.
You insert them into the table manually using a query like this:

INSERT INTO tblPeople VALUES(null, "Name", 23);

Notice: for auto-incrementing fields, a value of null causes it to auto-increment

Which in Java would look something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 // ----- create the Database -----
  2.               dbPeople= this.openOrCreateDatabase("dbPeople.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
  3.                 dbPeople.setLocale(Locale.getDefault());
  4.                 dbPeople.setLockingEnabled(true);
  5.                 dbPeople.setVersion(1);
  6.                
  7.                 //extract the data from the edit texts
  8.                 String strUserName = tbxName.getText().toString();
  9.                 int intUserAge = Integer.parseInt(tbxAge.getText.toString());
  10.                
  11.                 ContentValues values = new ContentValues();
  12.                 values.put("strName", strUserName);
  13.                 values.put("intAge", intUserAge);
  14.                 dbPeople.insert("tblPeople", null, values);
  15.        
  16.                 dbPeople.close();
  17.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


I use the ContentValues rather than inserting with a raw query because it can throw exceptions.
I also wrap these things into functions so you don't have to worry about them
User avatar
DMacATTACK
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Thu Dec 31, 2009 6:31 pm
Location: Canada

Re: How to save data in the database

Postby gurnoorinder » Fri Mar 25, 2011 5:24 am

thanks a lot for the reply...
Another thing that i want to ask is that what if i want to save just the first three letters of the user name instead of the entire name entered by the user?
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Re: How to save data in the database

Postby DMacATTACK » Fri Mar 25, 2011 4:54 pm

Then just parse the string from the EditText for the first three characters. And change your database VARCHAR(30) to VARCHAR(3).

String strName = tbxName.getText().toString.substring(0,3);
User avatar
DMacATTACK
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Thu Dec 31, 2009 6:31 pm
Location: Canada

Re: How to save data in the database

Postby gurnoorinder » Sat Mar 26, 2011 2:37 am

Thanks for the reply.... but when i try to do it i get NULL POINTER EXCEPTION.
Actually what i am trying to do is that i have age and name as edittext fields in one screen and i click SAVE and on the another screen it should display both the name and age, but i get NULL POINTER EXCEPTION.
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Re: How to save data in the database

Postby gurnoorinder » Sat Mar 26, 2011 2:54 am

having resolved the above issue, whenever i try to save it in the database, it is saving on NULL for both the columns. Even is i try to set the values of both the EditText fields via xml.

any idea where i am wrong
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Top

Re: How to save data in the database

Postby gurnoorinder » Sat Mar 26, 2011 10:29 am

ok now i knw that the code
EditText userNameTextField = (EditText)findViewById(R.id.savehighscoresid);
String userNameString = userNameTextField.getText().toString();

is not getting the value from the EditText field.
it is not able to store the value entered in the userNameString.

Any Idea's y this cud be...
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Re: How to save data in the database

Postby gurnoorinder » Sat Mar 26, 2011 11:12 am

it is storing null.... which means that no value is being obtained from the EditText Field

EditText userNameTextField = (EditText)findViewById(R.id.savehighscoresid);
String userNameString = userNameTextField.getText().toString();
gurnoorinder
Freshman
Freshman
 
Posts: 9
Joined: Thu Mar 24, 2011 10:12 pm

Re: How to save data in the database

Postby triplezero » Thu Mar 31, 2011 8:42 am

Hi..,
very simple,short and good tutorial.
how about to get the data?
triplezero
Freshman
Freshman
 
Posts: 6
Joined: Tue Mar 29, 2011 9:51 am

Re: How to save data in the database

Postby paco_deluca » Fri Apr 15, 2011 4:34 pm

this is great information.
paco_deluca
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 15, 2011 4:28 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 5 guests