Re: [android-developers] Need Help--- Regarding Creating Database and Tables

2010-04-28 Thread amsale zelalem
thank you so  much for the posted solution. it is of great help for me.
I want to ask you though some more questions.
1.  I would like to populate my tables statically(before even the app is 
launched) may be reading from a file onto the db. How can I do it?
2. I would like to add texts written in languages different from english. How 
can I do this too?
Please I am really in need of these two things and quick response would be 
appreciated.
thank you in advance

--- On Mon, 4/19/10, Liviu Ungureanu smartli...@gmail.com wrote:


From: Liviu Ungureanu smartli...@gmail.com
Subject: Re: [android-developers] Need Help--- Regarding Creating Database and 
Tables
To: android-developers@googlegroups.com
Date: Monday, April 19, 2010, 12:42 PM


Hi!


 I use this method to work with database:


 // this is my DatabaseManager class



package com.liviu.app.nearbyplace.data;


import java.util.ArrayList;


import com.liviu.app.nearbyplace.util.Constants;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;




public class DatabaseManager {

//data
private String TAG = DatabaseManager;
private SQLiteDatabase db;
private Context context;

public DatabaseManager(Context ctx) {

Log.e(TAG, DatabaseManager Constructor);
context = ctx;

openAndCreateDatabase();
closeDatabaseManager();
}

public boolean openAndCreateDatabase(){
try{

db = context.openOrCreateDatabase(Constants.DATABASE_NAME, 
Context.MODE_PRIVATE, null);
Log.e(TAG,Database is ready!);


                        // here I create my tables: 
db.execSQL(Constants.CREATE_SAVED_ITEMS_TABLE);  
db.execSQL(Constants.CREATE_HISTORY_TABLE);

return true;
}
catch (SQLException e){
Log.e(TAG,ERROR at accesing database!);
Log.e(TAG,e.toString());
return false;
} 
catch (IllegalStateException e) {
e.printStackTrace();
Log.e(TAG, database is not closed in openAndCreateDatabase());
closeDatabaseManager();
return false;
}
}

public boolean openDatabase(){

if(db != null  db.isOpen())
db.close();
try{
db = context.openOrCreateDatabase(Constants.DATABASE_NAME,Context.MODE_PRIVATE, 
null); 
return true;
}
catch (SQLException e){
Log.e(TAG,ERROR at accesing database!);
Log.e(TAG,e.toString());
return false;
}
catch (IllegalStateException e) {
e.printStackTrace();
Log.e(TAG, database is not closed in openDatabase());
closeDatabaseManager();
return openDatabase();
}

}

public void closeDatabaseManager(){

if(db.isOpen())
db.close();
else
Log.e(TAG,Database is not open!);
}


public boolean insertToHistory(String when, String what, int count) {
ContentValues values = new ContentValues(3);

values.put(Constants.DATE_FIELD, when);
values.put(Constants.ITEM_TITLE_FIELD, what);
values.put(Constants.ITEM_RESULTS_COUNT_FIELD, count);

long affectedRows = 0;
try{
affectedRows = db.insertOrThrow(Constants.TABLE_HISTORY, null, values);
Log.e(TAG, affectedRows:  + affectedRows);
if(affectedRows != -1)
return true;
else
return false;
}
catch (SQLException e) {
Log.e(TAG, nu am inserat in baza de date  + what +  count:  + count);
e.printStackTrace();
return false;
}
}


public ArrayListHistoryItem getSuggestions() {
Log.e(TAG, getSuggestions());
HistoryItem hItem;
ArrayListHistoryItem  suggestions;
String[] sProjection = new String[]{distinct  + Constants.ITEM_TITLE_FIELD, 
Constants.ITEM_RESULTS_COUNT_FIELD};
Cursor   cSuggestions   = db.query(Constants.TABLE_HISTORY,
 sProjection, 
 null, 
 null, 
 null, 
 null,
 null); 
if(cSuggestions == null){
Log.e(TAG, cSuggestions is null);
return null;
}

int numRows = cSuggestions.getCount();
suggestions = new ArrayListHistoryItem(numRows);
cSuggestions.moveToFirst(); 

for(int i = 0; i  numRows; i++){
hItem = new HistoryItem(cSuggestions.getString(0), cSuggestions.getInt(1)); 
suggestions.add(hItem);
Log.e(TAG, Suggestion:  + cSuggestions.getString(0) +  results count:  + 
cSuggestions.getInt(1));
cSuggestions.moveToNext();
}

Log.e(TAG, suggestions count:  + suggestions.size());
return suggestions;
} 

public String[] getSuggestionsAsArray() {
Log.e(TAG, getSuggestions());
String[]  suggestions;
String[] sProjection = new String[]{ distinct  + Constants.ITEM_TITLE_FIELD, 
Constants.ITEM_RESULTS_COUNT_FIELD };
Cursor   cSuggestions   = db.query(Constants.TABLE_HISTORY,
 sProjection, 
 null, 
 null, 
 null, 
 null,
 null); 
if(cSuggestions == null){
Log.e(TAG, cSuggestions is null);
return null;
}

int numRows = cSuggestions.getCount();
suggestions = new String[numRows];
cSuggestions.moveToFirst(); 

for(int i = 0; i  numRows; i++){ 
suggestions[i] = cSuggestions.getString(0);
Log.e(TAG, Suggestion:  + cSuggestions.getString(0) +  results count:  + 
cSuggestions.getInt(1));
cSuggestions.moveToNext();
}

Log.e(TAG, suggestions count:  + suggestions.length);
return suggestions;
}


public ArrayListString getHistoryDates() {
ArrayListString datesList;
int  numRows;
String[]  datesProjection

Re: [android-developers] Need Help--- Regarding Creating Database and Tables

2010-04-28 Thread Liviu Ungureanu
Hi! I'm happy to see this solution helped you.

For question 1:
 before even the app is launched I'm not sure if this is possible. To get
access to a database you need an context: you can get context just from
activities, services...You can try to do something like this: When your
application start for the first time, populate your tables with informations
from sdcard. Do not forget to show to user an progress dialog with some
informations about populating process.

For question 2.
 I you refer here to unicode strings, I do know if this is possible or not.
You have to try :).

Thank you!


2010/4/28 amsale zelalem amt...@yahoo.com

 thank you so  much for the posted solution. it is of great help for me.
 I want to ask you though some more questions.
 1.  I would like to populate my tables statically(before even the app is
 launched) may be reading from a file onto the db. How can I do it?
 2. I would like to add texts written in languages different from english.
 How can I do this too?
 Please I am really in need of these two things and quick response would be
 appreciated.
 thank you in advance

 --- On *Mon, 4/19/10, Liviu Ungureanu smartli...@gmail.com* wrote:


 From: Liviu Ungureanu smartli...@gmail.com
 Subject: Re: [android-developers] Need Help--- Regarding Creating Database
 and Tables
 To: android-developers@googlegroups.com
 Date: Monday, April 19, 2010, 12:42 PM

 Hi!

  I use this method to work with database:

  // this is my DatabaseManager class

  package com.liviu.app.nearbyplace.data;

 import java.util.ArrayList;

 import com.liviu.app.nearbyplace.util.Constants;

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.util.Log;


 public class DatabaseManager {
  //data
 private String TAG = DatabaseManager;
 private SQLiteDatabase db;
 private Context context;
  public DatabaseManager(Context ctx) {
  Log.e(TAG, DatabaseManager Constructor);
 context = ctx;
  openAndCreateDatabase();
 closeDatabaseManager();
 }
  public boolean openAndCreateDatabase(){
 try{
  db = context.openOrCreateDatabase(Constants.DATABASE_NAME,
 Context.MODE_PRIVATE, null);
 Log.e(TAG,Database is ready!);

 // here I create my tables:
 db.execSQL(Constants.CREATE_SAVED_ITEMS_TABLE);
 db.execSQL(Constants.CREATE_HISTORY_TABLE);
  return true;
 }
 catch (SQLException e){
 Log.e(TAG,ERROR at accesing database!);
 Log.e(TAG,e.toString());
 return false;
 }
 catch (IllegalStateException e) {
 e.printStackTrace();
 Log.e(TAG, database is not closed in openAndCreateDatabase());
 closeDatabaseManager();
 return false;
 }
 }
  public boolean openDatabase(){
  if(db != null  db.isOpen())
 db.close();
 try{
 db =
 context.openOrCreateDatabase(Constants.DATABASE_NAME,Context.MODE_PRIVATE,
 null);
 return true;
 }
 catch (SQLException e){
 Log.e(TAG,ERROR at accesing database!);
 Log.e(TAG,e.toString());
 return false;
 }
 catch (IllegalStateException e) {
 e.printStackTrace();
 Log.e(TAG, database is not closed in openDatabase());
 closeDatabaseManager();
 return openDatabase();
 }
  }
  public void closeDatabaseManager(){
  if(db.isOpen())
 db.close();
 else
 Log.e(TAG,Database is not open!);
 }

 public boolean insertToHistory(String when, String what, int count) {
 ContentValues values = new ContentValues(3);
  values.put(Constants.DATE_FIELD, when);
 values.put(Constants.ITEM_TITLE_FIELD, what);
 values.put(Constants.ITEM_RESULTS_COUNT_FIELD, count);
  long affectedRows = 0;
 try{
 affectedRows = db.insertOrThrow(Constants.TABLE_HISTORY, null, values);
 Log.e(TAG, affectedRows:  + affectedRows);
 if(affectedRows != -1)
 return true;
 else
 return false;
 }
 catch (SQLException e) {
 Log.e(TAG, nu am inserat in baza de date  + what +  count:  + count);
 e.printStackTrace();
 return false;
 }
 }

 public ArrayListHistoryItem getSuggestions() {
 Log.e(TAG, getSuggestions());
 HistoryItem hItem;
 ArrayListHistoryItem  suggestions;
 String[] sProjection = new String[]{distinct  +
 Constants.ITEM_TITLE_FIELD, Constants.ITEM_RESULTS_COUNT_FIELD};
 Cursor   cSuggestions   = db.query(Constants.TABLE_HISTORY,
  sProjection,
  null,
  null,
  null,
  null,
  null);
 if(cSuggestions == null){
 Log.e(TAG, cSuggestions is null);
 return null;
 }
  int numRows = cSuggestions.getCount();
 suggestions = new ArrayListHistoryItem(numRows);
 cSuggestions.moveToFirst();
  for(int i = 0; i  numRows; i++){
 hItem = new HistoryItem(cSuggestions.getString(0), cSuggestions.getInt(1));
 suggestions.add(hItem);
 Log.e(TAG, Suggestion:  + cSuggestions.getString(0) +  results count: 
 + cSuggestions.getInt(1));
 cSuggestions.moveToNext();
 }
  Log.e(TAG, suggestions count:  + suggestions.size());
 return suggestions;
 }
  public String[] getSuggestionsAsArray() {
 Log.e(TAG, getSuggestions());
 String[]  suggestions;
 String[] sProjection = new String[]{ distinct

Re: [android-developers] Need Help--- Regarding Creating Database and Tables

2010-04-19 Thread Liviu Ungureanu
Hi!

 I use this method to work with database:

 // this is my DatabaseManager class

package com.liviu.app.nearbyplace.data;

import java.util.ArrayList;

import com.liviu.app.nearbyplace.util.Constants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;


public class DatabaseManager {
 //data
private String  TAG = DatabaseManager;
private SQLiteDatabase db;
private Context context;
public DatabaseManager(Context ctx) {
 Log.e(TAG, DatabaseManager Constructor);
context = ctx;
 openAndCreateDatabase();
closeDatabaseManager();
}
 public boolean openAndCreateDatabase(){
try{
 db = context.openOrCreateDatabase(Constants.DATABASE_NAME,
Context.MODE_PRIVATE, null);
Log.e(TAG,Database is ready!);

// here I create my tables:
db.execSQL(Constants.CREATE_SAVED_ITEMS_TABLE);
db.execSQL(Constants.CREATE_HISTORY_TABLE);
 return true;
}
catch (SQLException e){
Log.e(TAG,ERROR at accesing database!);
Log.e(TAG,e.toString());
return false;
}
catch (IllegalStateException e) {
e.printStackTrace();
Log.e(TAG, database is not closed in openAndCreateDatabase());
closeDatabaseManager();
return false;
}
}
 public boolean openDatabase(){
 if(db != null  db.isOpen())
db.close();
try{
db =
context.openOrCreateDatabase(Constants.DATABASE_NAME,Context.MODE_PRIVATE,
null);
return true;
}
catch (SQLException e){
Log.e(TAG,ERROR at accesing database!);
Log.e(TAG,e.toString());
return false;
}
catch (IllegalStateException e) {
e.printStackTrace();
Log.e(TAG, database is not closed in openDatabase());
closeDatabaseManager();
return openDatabase();
}
 }
 public void closeDatabaseManager(){
 if(db.isOpen())
db.close();
else
Log.e(TAG,Database is not open!);
}

public boolean insertToHistory(String when, String what, int count) {
ContentValues values = new ContentValues(3);
 values.put(Constants.DATE_FIELD, when);
values.put(Constants.ITEM_TITLE_FIELD, what);
values.put(Constants.ITEM_RESULTS_COUNT_FIELD, count);
 long affectedRows = 0;
try{
affectedRows = db.insertOrThrow(Constants.TABLE_HISTORY, null, values);
Log.e(TAG, affectedRows:  + affectedRows);
if(affectedRows != -1)
return true;
else
return false;
}
catch (SQLException e) {
Log.e(TAG, nu am inserat in baza de date  + what +  count:  + count);
e.printStackTrace();
return false;
}
}

public ArrayListHistoryItem getSuggestions() {
Log.e(TAG, getSuggestions());
HistoryItem hItem;
ArrayListHistoryItem  suggestions;
String[] sProjection = new String[]{distinct  +
Constants.ITEM_TITLE_FIELD, Constants.ITEM_RESULTS_COUNT_FIELD};
Cursor   cSuggestions   = db.query(Constants.TABLE_HISTORY,
  sProjection,
  null,
  null,
  null,
  null,
  null);
if(cSuggestions == null){
Log.e(TAG, cSuggestions is null);
return null;
}
 int numRows = cSuggestions.getCount();
suggestions = new ArrayListHistoryItem(numRows);
cSuggestions.moveToFirst();
 for(int i = 0; i  numRows; i++){
hItem = new HistoryItem(cSuggestions.getString(0), cSuggestions.getInt(1));
suggestions.add(hItem);
Log.e(TAG, Suggestion:  + cSuggestions.getString(0) +  results count:  +
cSuggestions.getInt(1));
cSuggestions.moveToNext();
}
 Log.e(TAG, suggestions count:  + suggestions.size());
return suggestions;
}
 public String[] getSuggestionsAsArray() {
Log.e(TAG, getSuggestions());
String[]  suggestions;
String[] sProjection = new String[]{ distinct  +
Constants.ITEM_TITLE_FIELD, Constants.ITEM_RESULTS_COUNT_FIELD };
Cursor   cSuggestions   = db.query(Constants.TABLE_HISTORY,
  sProjection,
  null,
  null,
  null,
  null,
  null);
if(cSuggestions == null){
Log.e(TAG, cSuggestions is null);
return null;
}
 int numRows = cSuggestions.getCount();
suggestions = new String[numRows];
cSuggestions.moveToFirst();
 for(int i = 0; i  numRows; i++){
suggestions[i] = cSuggestions.getString(0);
Log.e(TAG, Suggestion:  + cSuggestions.getString(0) +  results count:  +
cSuggestions.getInt(1));
cSuggestions.moveToNext();
}
 Log.e(TAG, suggestions count:  + suggestions.length);
return suggestions;
}

public ArrayListString getHistoryDates() {
ArrayListString datesList;
int   numRows;
String[]   datesProjection= new String[]{ distinct  +
Constants.DATE_FIELD };
Cursor   dateCursor = db.query(Constants.TABLE_HISTORY,
datesProjection,
null,
null,
null,
null,
null);
if(dateCursor == null){
Log.e(TAG, dateCursor is null);
return new ArrayListString();
}
numRows = dateCursor.getCount();
datesList = new ArrayListString(numRows);
 dateCursor.moveToFirst();
 for(int i = 0; i  numRows; i++){
Log.e(TAG, date:  + dateCursor.getString(0));
datesList.add(dateCursor.getString(0));
dateCursor.moveToNext();
}
 Log.e(TAG, getHistoryDates() return  + datesList.size() +  dates);
 return datesList;
}

public ArrayListString getHistoryForDate(String date) {
ArrayListString hItemsList;
int numRows;
String[] hItemsProjection = new String[] {