You are not calling getWritableDatabase (a method inherited by your helper class). Hence, the db never gets created.
Might be a good idea to separate addEmployee, etc., into another class, so you have more clear separation between opening/creating the database vs. performing data operations with it -- Kostya Vasilyev 19.07.2011 16:32 пользователь "kirti waykole" <[email protected]> написал: > hello all, > I am new in android . I have problem with database. Plz see the below code. > database is not creating. Plz help me.... > thanksin advance > > this is my dBHelper class > > package com.example.android; > > > > import android.content.ContentValues; > import android.content.Context; > import android.database.Cursor; > import android.database.sqlite.SQLiteDatabase; > import android.database.sqlite.SQLiteOpenHelper; > > > > > public class DBHelper extends SQLiteOpenHelper { > > static final String dbName="demoDB"; > static final String employeeTable="Employees"; > > static final String colName="EmployeeName"; > String colDeptID = "EmployeeID"; > static final String viewEmps="ViewEmps"; > > > public DBHelper(Context context) { > super(context, dbName, null,33); > > // TODO Auto-generated constructor stub > } > > @Override > public void onCreate(SQLiteDatabase db) { > // TODO Auto-generated method stub > > > db.execSQL("CREATE TABLE "+employeeTable+" ("+colDeptID+ " INTEGER > PRIMARY KEY , "+ > colName+ " TEXT)"); > > > > } > > @Override > public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) > { > // TODO Auto-generated method stub > > db.execSQL("DROP TABLE IF EXISTS "+employeeTable); > //db.execSQL("DROP TABLE IF EXISTS "+deptTable); > > db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger"); > db.execSQL("DROP TRIGGER IF EXISTS dept_id_trigger22"); > db.execSQL("DROP TRIGGER IF EXISTS fk_empdept_deptid"); > db.execSQL("DROP VIEW IF EXISTS "+viewEmps); > onCreate(db); > } > > void AddEmployee(String name) > { > > > SQLiteDatabase db= this.getWritableDatabase(); > > > ContentValues cv=new ContentValues(); > > cv.put(colName,name); > > db.insert(employeeTable, colName, cv); > db.close(); > > > } > > int getEmployeeCount() > { > SQLiteDatabase db=this.getWritableDatabase(); > Cursor cur= db.rawQuery("Select * from "+employeeTable, null); > int x= cur.getCount(); > cur.close(); > return x; > } > > Cursor getAllEmployees() > { > SQLiteDatabase db=this.getWritableDatabase(); > > > > //Cursor cur= db.rawQuery("Select "+colID+" as _id , "+colName+", > "+colAge+" from "+employeeTable, new String [] {}); > Cursor cur= db.rawQuery("SELECT * FROM "+viewEmps,null); > return cur; > > } > > > } > > > > > this is my Activity class...... > > package com.example.android; > > import android.app.Activity; > import android.app.Dialog; > import android.database.Cursor; > import android.os.Bundle; > import android.text.Spannable; > import android.view.View; > import android.view.View.OnClickListener; > import android.widget.Button; > import android.widget.EditText; > import android.widget.SimpleCursorAdapter; > import android.widget.TextView; > > public class TryActivity extends Activity > { > EditText txtName; > > TextView txtEmps; > DBHelper dbHelper; > //Spinner spinDept; > > @Override > public void onCreate(Bundle savedInstanceState) { > super.onCreate(savedInstanceState); > setContentView(R.layout.main); > > txtName=(EditText)findViewById(R.id.nametext); > txtEmps=(TextView)findViewById(R.id.text); > > } > > @Override > public void onStart() > { > try > { > super.onStart(); > dbHelper=new DBHelper(this); > > txtEmps.setText(txtEmps.getText()+String.valueOf(dbHelper.getEmployeeCount())); > > } > > > //never close cursor > > catch(Exception ex) > { > CatchError(ex.toString()); > } > } > > public void btnAddEmp_Click(View view) > { > boolean ok=true; > try > { > Spannable spn=txtName.getText(); > String name=txtName.getText().toString(); > > dbHelper.AddEmployee(name); > > } > catch(Exception ex) > { > ok=false; > CatchError(ex.toString()); > } > finally > { > if(ok) > { > //NotifyEmpAdded(); > Alerts.ShowEmpAddedAlert(this); > txtEmps.setText("Number of employees > "+String.valueOf(dbHelper.getEmployeeCount())); > } > } > } > > void CatchError(String Exception) > { > Dialog diag=new Dialog(this); > diag.setTitle("Add new Employee"); > TextView txt=new TextView(this); > txt.setText(Exception); > diag.setContentView(txt); > diag.show(); > } > > void NotifyEmpAdded() > { > Dialog diag=new Dialog(this); > diag.setTitle("Add new Employee"); > TextView txt=new TextView(this); > txt.setText("Employee Added Successfully"); > diag.setContentView(txt); > diag.show(); > try { > diag.wait(1000); > } catch (InterruptedException e) { > // TODO Auto-generated catch block > CatchError(e.toString()); > } > diag.notify(); > diag.dismiss(); > } > } > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to [email protected] > To unsubscribe from this group, send email to > [email protected] > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

