Click here to Skip to main content
15,889,116 members
Articles / Mobile Apps / Android
Tip/Trick

Generator for SQLite Use in Android

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
13 Nov 2014CPOL 21.7K   788   8   9
Tools introduction for Android developers who use SQlite in coding

This is a reference link to file that will help to generate special scripts while coding in Android.

HTML
http://sourceforge.net/projects/sqlitehelperinandroid

Also one screen shot can be found.

To use it:

  1. Write a name for your table: people
    1. All code and functions will use this name
    2. You can save this script for later use, too (Ctrl+s). This will create Data folder and save script for later use.
    3. You may call this script by providing the above name and calling it by (Ctrl+L), it will load script from Folder Data.
  2. Define fields, select field type and write field name.
  3. Use Add(Ctrl-A) to add field to table script.
  4. For delete, click on table row and use Delete button, selected row will be deleted.
  5. 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:

C#
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:

C#
private static final String TABLE_CREATE_PEOPLE =
 "CREATE TABLE "+ TABLE_PEOPLE +" ("+
PEOPLE_ID + "INTEGER PRIMARY KEY, "+
PEOPLE_NAME + "TEXT, "+
PEOPLE_FAMILT + "TEXT)";

Modeling file:

C#
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:

C#
private static final String[] people_cols = {
OpenHelper. PEOPLE_ID,
OpenHelper. PEOPLE_NAME,
OpenHelper. PEOPLE_FAMILT,
};

SQL script:

SQL
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;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
I am developing Android apps, developing web service needed for apps in ASP.NET C#, and SQL, javascript, css, html coding.
I have been developing Delphi applications, and started decades ago from developing Foxpro.

Comments and Discussions

 
NewsGracias! Pin
Member 1112112113-Oct-15 6:27
Member 1112112113-Oct-15 6:27 
GeneralRe: Gracias! Pin
A.Ebrahimi19-May-16 1:14
A.Ebrahimi19-May-16 1:14 
QuestionThank you ! Pin
Member 1166519929-Jun-15 3:56
Member 1166519929-Jun-15 3:56 
AnswerRe: Thank you ! Pin
A.Ebrahimi19-May-16 1:16
A.Ebrahimi19-May-16 1:16 
QuestionAwesome! Pin
Member 1179855128-Jun-15 12:24
Member 1179855128-Jun-15 12:24 
AnswerRe: Awesome! Pin
A.Ebrahimi19-May-16 1:16
A.Ebrahimi19-May-16 1:16 
GeneralAmazing! Pin
Diego Fortes27-Jan-15 12:04
Diego Fortes27-Jan-15 12:04 
GeneralRe: Amazing! Pin
A.Ebrahimi27-Jan-15 17:43
A.Ebrahimi27-Jan-15 17:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.