I'm new at the android studio. I tried creating SQLite Database but when I try inserting data android studio cannot resolve it. i also tried MainActivity.this and getApplicationContext instead of db=new DatabaseHelper(this); on this line. and on device monitor, it isn't creating a database file
MainActivity.java
package com.example.lenovo.sqlite_2_deneme;
import android.content.Context;
import android.media.midi.MidiDevice;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db=new DatabaseHelper(this);
EditText et_name,et_surname,et_number;
Button btn_data;
et_name= (EditText) findViewById(R.id.et_isim);
et_surname= (EditText) findViewById(R.id.et_soyisim);
et_number= (EditText) findViewById(R.id.et_numara);
btn_data= (Button) findViewById(R.id.btn_veri);
btn_data.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.insertData();
}
});
}
}
and my DatabaseHelper class
package com.example.lenovo.sqlite_2_deneme;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.widget.TableLayout;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME= "student_info.db";
public static final String TABLE_NAME="student_table";
public static final String COL_1="ID";
public static final String COL_2="NAME";
public static final String COL_3="SURNAME";
public static final String COL_4="NUMBER";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE"+ TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, NUMBER INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
this.onCreate(db);
}
public Boolean DataInsert(String name, String surname, String number)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,surname);
contentValues.put(COL_4,number);
long sonuc= db.insert(TABLE_NAME,null,contentValues);
if(sonuc==-1)
return false;
else
return true;
}
}
Your database will be created at the instance when you call
SQLiteDatabase db=this.getWritableDatabase();
At the moment I dont see you have called this anywhere in your code. You have written a DataInsert method but it is not getting called in the code you have shared so far.
your'e sql query is wrong give the space in table (insert data),and also in onupgrade and there is no method insertData()
db.execSQL("CREATE TABLE "+ TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, NUMBER INTEGER)");
There is no method insertData() . You should use
DataInsert() method .
db=new DatabaseHelper(MainActivity.this);
EditText et_name,et_surname,et_number;
Button btn_data;
et_name= (EditText) findViewById(R.id.et_isim);
et_surname= (EditText) findViewById(R.id.et_soyisim);
et_number= (EditText) findViewById(R.id.et_numara);
btn_data= (Button) findViewById(R.id.btn_veri);
btn_data.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
db.DataInsert(et_name.getText().toString(),et_surname.getText().toString(),et_number.getText().toString());
}
});
FYI
void onCreate (SQLiteDatabase db)
Called when the database is created for the first time.
Related
This is my main activity and I'm getting exception at 29th line of insertData() in MainActivity method and MyDatabaseAdapter() class contains the schema.Can u please help me with where I'm getting the error?
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
MyDatabaseAdapter myDatabaseAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String name=editText.getText().toString();
long id=myDatabaseAdapter.insertData(name);
if(id<0)
{
Toast.makeText(MainActivity.this,"Unsuccessful",Toast.LENGTH_LONG);
}
else
{
Toast.makeText(MainActivity.this,"Successfull",Toast.LENGTH_LONG).show();
}
}
});
}
}
This is my sqlite helper class
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseAdapter {
MyDatabase myDatabase;
MyDatabaseAdapter(Context context)
{
myDatabase=new MyDatabase(context);
}
public long insertData(String name)
{
SQLiteDatabase sqLiteDatabase=myDatabase.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(MyDatabase.NAME,name);
long id=sqLiteDatabase.insert(myDatabase.TABLE_NAME,null,contentValues);
return id;
}
static class MyDatabase extends SQLiteOpenHelper
{
private static final String DATABASE_NAME="Mydatabase";
private static final int DATABASE_VERSION=3;
private static final String TABLE_NAME="MyContacts";
private static final String NAME="Name";
// private static final String ADDRESS="Address";
private static final String UID="_id";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),;";
private static final String DROP_TABLE="DROP TABLE IF EXISTS" +TABLE_NAME;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Toast.makeText(context,"Constructor is called",Toast.LENGTH_LONG).show();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
}
you did not instantiate your MyDatabaseAdapter class. Try
myDatabaseAdapter = new MyDatabaseAdapter(this) after setContentView().
Firstly, you should really be able to give us more information. Your logcat output should show exactly what the error is or run with the debugger to catch the exception as it happens. Anyhow, with the info provided, at least one problem is that your create statement has invalid syntax. It evaluates as:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255),;
The comma and semi-colon are not required and there is no closing bracket. It should be:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255))
Or more correctly:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name TEXT)
...because there is no VARCHAR type in SQLite. Type affinity converts your VARCHAR(255) to TEXT because it contains the string 'CHAR' but it is best to specify SQLite types only to ensure you get what you expect.
Sorry if I'm talking something wrong, I'm still learning.
But in line 29, considering that you have 2 lines for thepackage and one break, this is the line:
shouldn't long be used for numbers ?
long id=myDatabaseAdapter.insertData(name);
"long: The long data type is a 64-bit two's complement integer. The
signed long has a minimum value of -263 and a maximum value of 263-1.
In Java SE 8 and later, you can use the long data type to represent an
unsigned 64-bit long, which has a minimum value of 0 and a maximum
value of 264-1. Use this data type when you need a range of values
wider than those provided by int. The Long class also contains methods
like compareUnsigned, divideUnsigned etc to support arithmetic
operations for unsigned long."
I am new to Android Programming.. I am trying to create sqLite Database in android Here is my code
sqLitee.java
package com.example.sqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class sqLitee extends SQLiteOpenHelper
{
public static final String DB_NAME="student.db";
public static final String TABLE_NAME="student";
public static final String COL_1="ID";
public static final String COL_2="FIRST_NAME";
public static final String COL_3="LAST_NAME";
public static final String COL_4="MARKS";
public sqLitee(Context context)
{
super(context, DB_NAME,null, 1);
// TODO Auto-generated constructor stub
SQLiteDatabase db=this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTO INCREMENT, FIRST_NAME TEXT, LAST_NAME TEXT, MARKS INTEGER)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXIST " + TABLE_NAME);
onCreate(db);
}
}
MainActivity.java
package com.example.sqlite;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
sqLitee myDB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myDB = new sqLitee(this);
setContentView(R.layout.activity_main);
}
}
When I am trying to run it on Android virtual Device , It says unfortunately , sqLite has stopped..
Thanks
remove this line in your sqllite constructor:
SQLiteDatabase db=this.getWritableDatabase();
you try to get the db, which is currently initializing.
There's a syntax error in your CREATE TABLE SQL. AUTO INCREMENT should be AUTOINCREMENT.
As always, first see the logcat for exception stacktrace and to learn what is failing and where.
My database class is DB.java
package com.example.pocketbooker;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DB extends SQLiteOpenHelper implements BaseColumns{
private static final String DATABASE_NAME = "pb_database.db";
private static final int DATABASE_VERSION = 1;
public DB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Members(_id INTEGER PRIMARY KEY AUTOINCREMENT, Mem_Name VARCHAR(50));");
db.execSQL("CREATE TABLE Incomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Inc_Name VARCHAR(50), Inc_Sum VARCHAR(10), Inc_Mem VARCHAR (50), Inc_Change_Date DATE, " +
"Inc_Perbeg_Date DATE, Inc_Period VARCHAR (4));");
db.execSQL("CREATE TABLE Outcomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Out_Name VARCHAR(50), Out_Sum VARCHAR(10), Out_Mem VARCHAR (50), Out_Change_Date DATE, " +
"Out_Perbeg_Date DATE, Out_Period VARCHAR (4));");
db.execSQL("CREATE TABLE Goals(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Goal_Name VARCHAR(50), Goal_Sum VARCHAR(10), Goal_Mem VARCHAR (50), Goal_Change_Date DATE, " +
"Goal_Perbeg_Date DATE, Goal_Period VARCHAR (4));");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
// TODO Auto-generated method stub
}
}
My "other" class is Dialog_mem.java
package com.example.pocketbooker;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
#SuppressLint({ "NewApi", "ValidFragment" })
public class Dialog_mem extends DialogFragment implements OnClickListener {
EditText memname;
ContentValues cv = new ContentValues();
private SQLiteDatabase database;
private DB dbHelper;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle("Добавить Члена Семьи");
View v = inflater.inflate(R.layout.mem_dialog, null);
v.findViewById(R.id.mem_btnOK).setOnClickListener(this);
v.findViewById(R.id.mem_btnCancel).setOnClickListener(this);
memname=(EditText) v.findViewById(R.id.mem_name);
return v;
}
public void onClick(View v) {
switch(v.getId())
{ case R.id.mem_btnOK:
database = dbHelper.getWritableDatabase();
cv.put("Mem_Name", memname.getText().toString());
database.insert("Members", "Mem_Name", cv);
database.close();
Toast.makeText(getActivity(), "Добавлено", Toast.LENGTH_SHORT).show();
default:
dismiss();}
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}
}
Eclipse shows NullPointerException. I guess it's context error, but I don't know how to point the context needed. getActivity() is the wrong one. "This" too. getApplicationContext() doesn't work at all.
You need to initialize the dbHelper instance.
What you can do is
1. Create a singleton class for all Database operations.
2. Create a Datasource class. Through which you will access the Database.
3. In Data Source class add open() method
/**
* Open the Database
* #throws SQLException
*/
public void open() throws SQLException {
if (dbHelper != null) {
db = dbHelper.getWritableDatabase();
}
}
/**
* Close
*/
public void close() {
dbHelper.close();
}
4. Write the method which you need to acces Database in DataSource class and call the database operations on this db instance.
Well, I used the akashsr's answer (thank you, man, for the idea). If anyone is interested how I've made it (at last) here's the code.
DB.java
package com.example.pocketbooker;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DB extends SQLiteOpenHelper implements BaseColumns{
private static final String DATABASE_NAME = "pb_database.db";
private static final int DATABASE_VERSION = 1;
private static DB sInstance;
ContentValues cv;
public DB(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE Members(_id INTEGER PRIMARY KEY AUTOINCREMENT, Mem_Name VARCHAR(50));");
db.execSQL("CREATE TABLE Incomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Inc_Name VARCHAR(50), Inc_Sum VARCHAR(10), Inc_Mem VARCHAR (50), Inc_Change_Date DATE, " +
"Inc_Perbeg_Date DATE, Inc_Period VARCHAR (4));");
db.execSQL("CREATE TABLE Outcomes(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Out_Name VARCHAR(50), Out_Sum VARCHAR(10), Out_Mem VARCHAR (50), Out_Change_Date DATE, " +
"Out_Perbeg_Date DATE, Out_Period VARCHAR (4));");
db.execSQL("CREATE TABLE Goals(_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"Goal_Name VARCHAR(50), Goal_Sum VARCHAR(10), Goal_Mem VARCHAR (50), Goal_Change_Date DATE, " +
"Goal_Perbeg_Date DATE, Goal_Period VARCHAR (4));");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
// TODO Auto-generated method stub
}
}
Created DataSource class with insert method. PBDataSource.java
package com.example.pocketbooker;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class PBDataSource {
DB dbHelper;
SQLiteDatabase db;
public PBDataSource(Context context) {
dbHelper = new DB(context);
}
public void open() throws SQLException {
if (dbHelper != null) {
db = dbHelper.getWritableDatabase();
}
}
public void close() {
dbHelper.close();
}
public void insertrecord (String a, String b, String c)
{ContentValues cv= new ContentValues();
cv.put(b,c);
db.insert(a, null, cv);
}
}
And used this method in my DialogFragment class Dialog_mem.java
package com.example.pocketbooker;
import android.annotation.SuppressLint;
import android.app.DialogFragment;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
#SuppressLint({ "NewApi", "ValidFragment" })
public class Dialog_mem extends DialogFragment implements OnClickListener {
EditText memname;
private PBDataSource datasource;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().setTitle("Добавить Члена Семьи");
View v = inflater.inflate(R.layout.mem_dialog, null);
v.findViewById(R.id.mem_btnOK).setOnClickListener(this);
v.findViewById(R.id.mem_btnCancel).setOnClickListener(this);
memname=(EditText) v.findViewById(R.id.mem_name);
datasource = new PBDataSource(getActivity());
return v;
}
public void onClick(View v) {
switch(v.getId())
{ case R.id.mem_btnOK:
datasource.open();
datasource.insertrecord("Members","Mem_Name", memname.getText().toString());
datasource.close();
Toast.makeText(getActivity(), "Добавлено", Toast.LENGTH_SHORT).show();
default:
dismiss();}
}
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
}}
Of course, I'm a noob. I try to study myself as I can. And I hope it will help someone like me.
i am trying to implement an app in which text entered in edittext is saved to the DB and then is displayed in a text field. But the app is crashing when i try to do this and i get a null pointer exception. This is my code
package com.example.dbex;
import java.sql.SQLOutput;
import java.sql.SQLPermission;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DBHandler extends SQLiteOpenHelper {
public static final int DATABASE_VERSION=1;
public static final String DATABASE_NAME="contactsmanager";
public static final String TABLE_CONTACTS="contacts";
public static final String KEY_ID="id";
public static final String KEY_NAME="name";
public static final String KEY_NUMBER="number";
public DBHandler(Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_TABLE_CONTACTS="CREATE TABLE "+TABLE_CONTACTS+"("+KEY_ID+" INTEGER PRIMARY KEY,"+KEY_NAME+" TEXT,"+KEY_NUMBER+" TEXT"+")";
db.execSQL(CREATE_TABLE_CONTACTS);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+TABLE_CONTACTS);
onCreate(db);
}
public void addContact(Contacts contacts)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_NAME, contacts.getname());
values.put(KEY_NUMBER, contacts.getnumber());
db.insert(TABLE_CONTACTS,null,values);
db.close();
}
public Contacts getContact(int id)
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor c=db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_NAME,KEY_NUMBER},KEY_ID+" =?",new String[]{String.valueOf(id)}, null, null, null, null);
Contacts contacts=null;
if(c!=null&&c.moveToFirst())
{
contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
}
return contacts;
}
public void deleteContact(Contacts contacts)
{
SQLiteDatabase db=this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID+" =?", new String[]{String.valueOf(contacts.getID())});
db.close();
}
}
and the main activity is
package com.example.dbex;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.button1);
EditText et1=(EditText)findViewById(R.id.editText1);
EditText et2=(EditText)findViewById(R.id.editText2);
final TextView tv1=(TextView)findViewById(R.id.textView1);
final String _name=et1.getText().toString();
final String _phone=et2.getText().toString();
final DBHandler dbh=new DBHandler(getBaseContext());
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dbh.addContact(new Contacts(_name, _phone));
String contact1=(dbh.getContact(0)).getname();
tv1.setText(contact1);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
i am getting the null pointer exception in the line
String contact1=(dbh.getContact(0)).getname();
where is my mistake?
thanks
Since you are telling you are able to insert the value, I assume your DB creation is correct You have hardcoded the id being queried to 0. You may not be having any value here. See this post to understand that even when you delete existing rows the auto increment ID could be continuing from the last used value instead of 0. So you could be returning a null value in this part of your code:
Contacts contacts=null;
if(c!=null&&c.moveToFirst())
{
contacts=new Contacts(Integer.parseInt(c.getString(0)),c.getString(1),c.getString(2));
}
return contacts;
I have four EditText and two buttons namely, save and review button.
If i press the save button EditText value is stored in to the database and on clicking the review button saved value in the database is listed in ListView.
How is this done please anybody explain to me.
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, "MedicinePill", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS names3 ("
+ BaseColumns._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, first VARCHAR, last VARCHAR ,dose2 VARCHAR ,dose3 VARCHAR)");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Steps to upgrade the database for the new version ...
}
}
//this is my database class in this where i add the insert data from the database
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Medical extends Activity
{
Button AddMedicine,ReviewMedicine;
EditText dose1,dose2,dose3;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AddMedicine = (Button)findViewById(R.id.add);
ReviewMedicine = (Button)findViewById(R.id.rev);
dose1 = (EditText)findViewById(R.id.Edit1);
AddMedicine.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String s1 = dose1.getText().toString();
// TODO Auto-generated method stub
}
});
}
}
//and this is my activity class
how i add edittext value in to database pls any body help me!!!!!!!!
You need to create class insertdose in DatabaseHelper like this:
public long insertdose(String dose1)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_dose, dose1);
return db.insert(DATABASE_TABLE, null, initialValues);
}
and call it at your onClick view
You will need to create a method in the DatabaseHelper class that takes in four strings and adds them to your database.
Then in your onClick() method in your Medical activity class you can get the string values from your 4 EditText fields and call your method with these values.
I would recommend going through the Notepad Tutorial. This should provide a good example of what you want to do.