Skip to main content

Posts

Showing posts with the label database

Connecting to a remote mysql database using php and Android?

Connecting to a remote database has been a Herculean Task for some programmers. No more worries here is a simple post to make it really simple. Follow these steps exactly to get it done. Remember these when you are working with an online database. 1. Make sure that you have a running server. 2. INTERNET permission should be present in the AndroidManifest file of your app. 3. The path that you give in your program to point to the server file should be correct. After ensuring all these you are ready to go. First we will start with the Server side. Here I am going with a PHP example. I am getting a value from the Android side by using a variable say "my_val". Then the PHP script will look like this. Note that here I am only sending one value from the Android side to the PHP Side. so the Server side code will look like this. $val = $_POST['my_val']; echo $val; Look at the varaible name inside the $_POST. Make sure that you use the same variable name ...

Use Your Own SQLite in ANDROID Application or Using 'SQLite Asset Helper' to read your own databases stored in Asset folder

See more basics on Android along with interview questions See SQLite tutorial here If you want to read your own databases in ANDROID APPLICATION, this post may help you. You may be familiar by creating SQLite in Android use  SQLiteOpenHelper . Here we are going to deal with using our own databases.  I have a database named 'mydatabase.sqlite', which consist of a set of name and age of 5 students. Suppose I want to view the database, I can use SQLite database browser which you can download from here.                                                     download browser..... Through this browser we can view the databases, can even modify delete or create a new one. 'mydatabase.sqlite' with name and age of five students is browsed and viewed as shown below Now  make a zip format of database and copy this database to asse...

A complete reference to Database Helper Class in SQLite in ANDROID

For COMPLETE TUTORIALS see here : DABASEHELPER CLASS : import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { public static final String KEY_ROWID = "id"; public static final String KEY_NAME = "title"; public static final String KEY_NICKNAME = "duedate"; private static final String DATABASE_NAME = "assinDB.db"; private static final String DATABASE_TABLE = "assignments"; private static final int DATABASE_VERSION = 2; private final Context context; private DatabaseHelper ObjectDBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { context = ctx; } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context con...

Get a single record from SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DATABASE HELPER class: String[] coloumns = new String[] { KEY_ROWID, KEY_NAME, KEY_NICKNAME }; // calling elements in an array Cursor c = db.query(DATABASE_TABLE, coloumns, KEY_ROWID + "=" + l,null, null, null, null); if (c != null) { c.moveToFirst(); String name = c.getString(1); // since name is in position 1 ie second coloumn return name; } return null; in sqlite.java : String s =editinfo.getText().toString(); long l=Long.parseLong(s); DBAdapter adapter=new DBAdapter(SQliteExample.this); adapter.open(); String returnedname=adapter.returnName(l); String returnednickname=adapter.returnickname(l); adapter.close(); editName.setText(returnedname); editNickName.setText(returnednickname);

Get all records from SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : String[] coloumns = new String[] { KEY_ROWID, KEY_NAME,KEY_NICKNAME }; // calling all elements in an array Cursor c = db.query(DATABASE_TABLE, coloumns, null, null, null, null,null);// basically reading a database need cursor String result = " ";// since string is to be returned System.out.println("count of cursor=====>>" + c.getCount()); int iRowid = c.getColumnIndex(KEY_ROWID); // calling each row values int iRowName = c.getColumnIndex(KEY_NAME); int iRowNickName = c.getColumnIndex(KEY_NICKNAME); if (c.moveToFirst()) { c.moveToFirst(); for (int i = 0; i " + iRowid + " "+ iRowName); c.moveToNext(); } } c.close(); db.close(); return result; in sqlite.java : TextView infotext=(TextView) findViewById(R.id.tvsqlinfo); DBAdapter info = new DBAdapter(this); ...

Modify records in SQLite in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : ContentValues cvupdate = new ContentValues(); // Bundle for writting the database fields cvupdate.put(KEY_NAME, namestr);// put the values passed to ContentValues cvupdate.put(KEY_NICKNAME, nickstr);// put the values passed to ContentValues db.update(DATABASE_TABLE, cvupdate, KEY_ROWID + "=" + smodify, null);// specify where to be changed in sqlite.java : String Namestr = editName.getText().toString(); String Nickstr = editNickName.getText().toString(); String smodify =editinfo.getText().toString(); long lmodify =Long.parseLong(smodify); DBAdapter adaptmodify =new DBAdapter(SQliteExample.this); adaptmodify.open(); adaptmodify.updateentry(lmodify,Namestr,Nickstr); // pass index and new values to be inserted adaptmodify.close();

Delete records from SQLite table in ANDROID

For COMPLETE TUTORIALS see here : in DABASEHELPER CLASS : public void deleteentry(long ldelete) { // TODO Auto-generated method stub db.delete(DATABASE_TABLE, KEY_ROWID + "=" +ldelete, null); } in Sqlite.java : String sdelete =editinfo.getText().toString(); long ldelete =Long.parseLong(sdelete); DBAdapter adaptdelete =new DBAdapter(SQliteExample.this); adaptdelete.open(); adaptdelete.deleteentry(ldelete); //pass the index to be deleted adaptdelete.close();

Insert records in SQLite tables in ANDROID

For COMPLETE TUTORIAL see here : in DABASEHELPER CLASS : public void entryfield(String namestr, String nickstr) { ContentValues cv = new ContentValues(); // Bundle for writting the database fields cv.put(KEY_NAME, namestr);// put the values passed to ContentValues cv.put(KEY_NICKNAME, nickstr);// put the values passed to ContentValues db.insert(DATABASE_TABLE, null, cv);// insert into databse with vlues as "content vales" } in sqlite.java //==Create an object of adapter class to call methods== DBAdapter Entryadapter = new DBAdapter(SQliteExample.this); Entryadapter.open(); Entryadapter.entryfield(Namestr,Nickstr); // Calling the function in DBAdapter Class Entryadapter.close();

Simple SQLite Example in Android

See more basics on Android along with interview questions DBAdapter.java (DABASEHELPER CLASS) import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBAdapter { public static final String KEY_ROWID = "id"; public static final String KEY_NAME = "title"; public static final String KEY_NICKNAME = "duedate"; private static final String DATABASE_NAME = "assinDB.db"; private static final String DATABASE_TABLE = "assignments"; private static final int DATABASE_VERSION = 2; private final Context context; private DatabaseHelper ObjectDBHelper; private SQLiteDatabase db; public DBAdapter(Context ctx) { context = ctx; } private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context ...