This is a reference link to file that will help to generate special scripts while coding in Android.
http://sourceforge.net/projects/sqlitehelperinandroid
Also one screen shot can be found.
To use it:
- Write a name for your table:
people
- All code and functions will use this name
- You can save this script for later use, too (Ctrl+s). This will create Data folder and save script for later use.
- You may call this script by providing the above name and calling it by (Ctrl+L), it will load script from Folder Data.
- Define fields, select field type and write field name.
- Use Add(Ctrl-A) to add field to table script.
- For delete, click on table row and use Delete button, selected row will be deleted.
- For edit, click on row, change name or type of field, and click Edit button to save changes.
Here is the sample code generated.
Define helper:
public static final String TABLE_PEOPLE = people;
public static final String PEOPLE_ID = id;
public static final String PEOPLE_NAME = name;
public static final String PEOPLE_FAMILT = familt;
Create table helper:
private static final String TABLE_CREATE_PEOPLE =
"CREATE TABLE "+ TABLE_PEOPLE +" ("+
PEOPLE_ID + "INTEGER PRIMARY KEY, "+
PEOPLE_NAME + "TEXT, "+
PEOPLE_FAMILT + "TEXT)";
Modeling file:
import android .os.Parcel;
import android .os.Parcelable;
public class People implements Parcelable {
private Long id;
private String name;
private String familt;
public Long getID() {
return id;
}
public void setID(Long id) {
this.id = id;
}
public String getNAME() {
return name;
}
public void setNAME(String name) {
this.name = name;
}
public String getFAMILT() {
return familt;
}
public void setFAMILT(String familt) {
this.familt = familt;
}
@Override
public String toString(){
return "";
}
public People(){
}
public People( Parcel in){
id = in.readLong();
name = in.readString();
familt = in.readString();
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags){
dest.writeLong(id);
dest.writeString(name);
dest.writeString(familt);
}
public static final Parcelable.Creator<People> CREATOR =
new Parcelable.Creator<People>(){
@Override
public People createFromParcel(Parcel source){
return new People(source);
}
@Override
public People[] newArray(int size){
return new People[size];
}
};
}
Columns definition:
private static final String[] people_cols = {
OpenHelper. PEOPLE_ID,
OpenHelper. PEOPLE_NAME,
OpenHelper. PEOPLE_FAMILT,
};
SQL script:
public People insertPeople(People people){
ContentValues values = new ContentValues();
values.put(OpenHelper.PEOPLE_ID, people.getID());
values.put(OpenHelper.PEOPLE_NAME, people.getNAME());
values.put(OpenHelper.PEOPLE_FAMILT, people.getFAMILT());
long insertId = database.insert(OpenHelper.TABLE_PEOPLE, null,values);
people.setID(insertId);
return people;
}
public int updatePeople(People people){
ContentValues values = new ContentValues();
values.put(OpenHelper.PEOPLE_ID, people.getID());
values.put(OpenHelper.PEOPLE_NAME, people.getNAME());
values.put(OpenHelper.PEOPLE_FAMILT, people.getFAMILT());
String where = OpenHelper.PEOPLE_ID +"="+ people.getID();
int result = database.update(OpenHelper.TABLE_PEOPLE, values, where, null);
return result;
}
public boolean deletePeople(People people){
String where = OpenHelper.PEOPLE_ID +"="+ people.getID();
int result = database.delete(OpenHelper.TABLE_PEOPLE, where, null);
return (result==1);
}
public People findPeople(String where){
Cursor cursor = database.query(OpenHelper.TABLE_PEOPLE,people_cols, where, null, null, null, null);
if (cursor.getCount()>0){
People people = new People();
cursor.moveToFirst();
people.setID(cursor.getLong(cursor.getColumnIndex(OpenHelper.PEOPLE_ID)));
people.setNAME(cursor.getString(cursor.getColumnIndex(OpenHelper.PEOPLE_NAME)));
people.setFAMILT(cursor.getString(cursor.getColumnIndex(OpenHelper.PEOPLE_FAMILT)));
return people;
} else return null;
}
public List<People> findAllPeople(String where){
List<People> peoples = new ArrayList<People>();
Cursor cursor = database.query(OpenHelper.TABLE_PEOPLE,people_cols, where, null, null, null, null);
if (cursor.getCount()>0){
while (cursor.moveToNext()){
People people = new People();
people.setID(cursor.getLong(cursor.getColumnIndex(OpenHelper.PEOPLE_ID)));
people.setNAME(cursor.getString(cursor.getColumnIndex(OpenHelper.PEOPLE_NAME)));
people.setFAMILT(cursor.getString(cursor.getColumnIndex(OpenHelper.PEOPLE_FAMILT)));
peoples.add(people);
}
}
return peoples;
}