I can't insert data - android

I'm making an app to insert data. But when I click on add button by giving all the details. App return me to previous page
This is the way I create insert class
public class InsertStudent extends AppCompatActivity {
Button instudent;
DBHelper dbHelper;
EditText sName,sDOB,sAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert_student);
instudent = findViewById(R.id.btninsert);
sName = findViewById(R.id.insertname);
sDOB = findViewById(R.id.insertdob)
;
sAddress = findViewById(R.id.insertaddress);
Below is the way I coded to insert data
instudent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String userName = sName.getText().toString();
String dateB = sDOB.getText().toString();
String addr = sAddress.getText().toString();
boolean count = dbHelper.addInfo(userName,dateB,addr );
if(count =true){
Toast.makeText(InsertStudent.this, "Inserted!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(InsertStudent.this, "Something went wrong!", Toast.LENGTH_SHORT).show();
}
}
});
This is addinfo method in DBHelper class
public boolean addInfo(String stdName, String stdDOB, String stdAddress){
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(UserProfile.Users.COLUMN_STDNAME, stdName);
contentValues.put(UserProfile.Users.COLUMN_DATEOFBIRTH, stdDOB);
contentValues.put(UserProfile.Users.TABLE_ADDRESS, stdAddress);
long result = sqLiteDatabase.insert(UserProfile.Users.TABLE_NAME, null, contentValues);
if(result==1)
return false;
else
return true;
}
}

The insert method of "SQLiteDatabase" class doesn't return the
count, it's returns the id of the inserted row. so you are checking
if return result is 1, it's a true process, but it's not a way to
check the insert method. It means you need to check if there is any
return result, your insert action performed successfully, but if
there is a problem, the application will crash.
Make sure you created the table that you want to insert data in it.

Related

insert bitmap image in sqlite database

I have a database app. It shows a list of items containing an image and strings. I want to store image in sqlite database in an activity and fetch it from another activity. But it shows null value is inserted. Here is the code for insertion of images in database---
public class EditorActivity extends AppCompatActivity implements View.OnClickListener{
private static int IMAGE_GALLERY_REQUEST=20;
private EditText mNameEditText;
private EditText mDescEditText;
private EditText mResEditText;
private EditText mStatusEditText;
private Button btn;
private byte[] b;
private Bitmap bitmap;
String mName,mDescription,mResident,mStatus;
int data;
public static String EXTRA_DATA="dataNo";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_editor);
mNameEditText = (EditText) findViewById(R.id.name);
mDescEditText = (EditText) findViewById(R.id.desc);
mResEditText = (EditText) findViewById(R.id.res);
mStatusEditText = (EditText) findViewById(R.id.status);
btn=findViewById(R.id.photo);
btn.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
data = (Integer) (bundle.get(EXTRA_DATA));
}
}
private void saveData()
{
mName=mNameEditText.getText().toString().trim();
mDescription=mDescEditText.getText().toString().trim();
mResident=mResEditText.getText().toString().trim();
mStatus=mStatusEditText.getText().toString().trim();
FriendsDbHelper helper=new FriendsDbHelper(this);
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(FriendContract.FriendEntry.NAME,mName);
values.put(FriendContract.FriendEntry.DESCRIPTION,mDescription);
values.put(FriendContract.FriendEntry.RESIDENCE,mResident);
values.put(FriendContract.FriendEntry.STATUS,mStatus);
values.put(FriendContract.FriendEntry.KEY_IMAGE,b);
db.insert(TABLE_NAME,null,values);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_edit,menu);
return true;
}
public void updateData() {
String name=mNameEditText.getText().toString().trim();
String description=mDescEditText.getText().toString().trim();
String resident=mResEditText.getText().toString().trim();
String status=mStatusEditText.getText().toString().trim();
//try{
FriendsDbHelper helper = new FriendsDbHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
if(TextUtils.isEmpty(name))
{
name=mName;
}
if(TextUtils.isEmpty(description))
{
description=mDescription;
}
if(TextUtils.isEmpty(resident))
{
resident=mResident;
}
if(TextUtils.isEmpty(status))
{
status=mStatus;
}
values.put(NAME, name);
values.put(DESCRIPTION, description);
values.put(RESIDENCE, resident);
values.put(STATUS, status);
db.update(TABLE_NAME, values, _ID + "=?", new String[]{Integer.toString(data)});
/* }
catch (SQLiteException e)
{
Toast.makeText(this,"Update failed",Toast.LENGTH_LONG).show();
}*/
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId())
{
case R.id.action_save:
saveData();
finish();
return true;
case R.id.action_update:
updateData();
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View v) {
Intent photoIntent=new Intent(Intent.ACTION_PICK);
File photoDirectory= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String photo=photoDirectory.getPath();
Uri uri=Uri.parse(photo);
photoIntent.setDataAndType(uri,"image/*");
startActivityForResult(photoIntent,IMAGE_GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
if(resultCode==IMAGE_GALLERY_REQUEST)
{
Uri uri=data.getData();
InputStream inputStream;
try
{
inputStream=getContentResolver().openInputStream(uri);
bitmap= BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,0,stream);
b=stream.toByteArray();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Unable to open image",Toast.LENGTH_LONG).show();
}
}
}
}
}
As a result, strings in each element is visible but image is not visible.
I read same problem from
Insert bitmap to sqlite database. They told to use sqlitemaestro software. How to use that in android?
Please reply soon.
It seems as if the Byte array 'b' is null when you are using the values.put in the savedata method kindly log the variable and check.
Also to save image in SQLite we usually make use of Binary Large Objects i.e BLOBs
Here is a resource which may help you
How to store(bitmap image) and retrieve image from sqlite database in android?
Using a cutdown version of your code. There appears to be nothing wrong with the SQLite side. That is using :-
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static int IMAGE_GALLERY_REQUEST = 20;
private EditText mNameEditText;
private EditText mDescEditText;
private EditText mResEditText;
private EditText mStatusEditText;
private Button btn;
private byte[] b;
private Bitmap bitmap;
String mName, mDescription, mResident, mStatus;
int data;
public static String EXTRA_DATA = "dataNo";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNameEditText = (EditText) findViewById(R.id.name);
mDescEditText = (EditText) findViewById(R.id.desc);
mResEditText = (EditText) findViewById(R.id.res);
mStatusEditText = (EditText) findViewById(R.id.status);
btn = findViewById(R.id.photo);
btn.setOnClickListener(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
data = (Integer) (bundle.get(EXTRA_DATA));
}
}
private void saveData() {
mName = mNameEditText.getText().toString().trim();
mDescription = mDescEditText.getText().toString().trim();
mResident = mResEditText.getText().toString().trim();
mStatus = mStatusEditText.getText().toString().trim();
FriendsDbHelper helper = new FriendsDbHelper(this);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FriendsDbHelper.COL_FRIENDS_NAME, mName);
values.put(FriendsDbHelper.COL_FRIENDS_DESCRIPTION, mDescription);
values.put(FriendsDbHelper.COL_FRIENDS_RESIDENCE, mResident);
values.put(FriendsDbHelper.COL_FRIENDS_STATUS, mStatus);
values.put(FriendsDbHelper.COL_FRIENDS_KEY_IMAGE, b);
db.insert(FriendsDbHelper.TB_FRIENDS, null, values);
}
#Override
public void onClick(View v) {
if (v.getId() == btn.getId()) {
b = new byte[]{0,2,3,4,5,6,7,8,9};
saveData();
}
}
}
Saves a BLOB as expected, as per :-
i.e. the last column is the array of bytes ( the elements being 0,2,3,4,5,6,7,8,9) shown as hex representation as was expected.
This therefore rules out any issue with the SQLite aspect and therefore indicates that the issue is that b is not being set accordingly in the onActivityResult method, which itself relies upon the intent.ACTION_PICK.
You may wish to refer to opening an image using Intent.ACTION_PICK (see notes re returning null) and perhaps Intent.ACTION_PICK behaves differently or the many other SO questions regarding intent.ACTION_PICK.
You may also wish to use something like :-
private void saveData()
{
mName=mNameEditText.getText().toString().trim();
mDescription=mDescEditText.getText().toString().trim();
mResident=mResEditText.getText().toString().trim();
mStatus=mStatusEditText.getText().toString().trim();
FriendsDbHelper helper=new FriendsDbHelper(this);
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(FriendContract.FriendEntry.NAME,mName);
values.put(FriendContract.FriendEntry.DESCRIPTION,mDescription);
values.put(FriendContract.FriendEntry.RESIDENCE,mResident);
values.put(FriendContract.FriendEntry.STATUS,mStatus);
values.put(FriendContract.FriendEntry.KEY_IMAGE,b);
//<<<< ADDED to issue Toast if no valid image...
if (b == null) {
Toast.makeText(this,"No valid Image - No Data Stored!",Toast.LENGTH_LONG).show();
return;
}
db.insert(TABLE_NAME,null,values);
}

Updating my Column in SQLite with Android

I have a method in my activity class which should print a random role to the player (stores in an SQLite database). I am getting a success message but it is not being carried out. I only have 1 record in my SQLite database so far and will be adding a while loop after to populate each row.
This is my my activity class:
public class StartGame extends AppCompatActivity implements View.OnClickListener {
DatabaseHelper myDb;
Button btnRoles;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_startgame);
myDb = new DatabaseHelper(this);
btnRoles = (Button) findViewById(R.id.btnAssignRoles);
assignRoles();
}
public String RandomNumber() {
List < String > roles = Arrays.asList("Mafia", "Mafia", "Angel", "Detective", "Civilian", "Civilian", "Civilian");
Collections.shuffle(roles);
return roles.get(0);
}
public void assignRoles() {
btnRoles.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
{
boolean isUpdated = myDb.updateRole(RandomNumber().toString());
if (isUpdated == true)
Toast.makeText(StartGame.this, "Roles assigned, keep them secret!", Toast.LENGTH_LONG).show();
else
Toast.makeText(StartGame.this, "UNSUCCESSFUL!", Toast.LENGTH_LONG).show();
}
}
}
);
}
And this is the method in my Database Helper class:
public boolean updateRole(String role){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_ROLE, role);
db.update(TABLE_NAME, contentValues, "Role =?", new String[] {role});
return true;
}
What am I doing wrong?
You got an error in this line:
db.update(TABLE_NAME, contentValues, "Role =?", new String[] {role});
You are updating all the rows in the table where Role = {role} to have the column Role the value {role}. So obviously this will have no effect.
You need to have some thing like id and use that in your where statement, some thing like this:
db.update(TABLE_NAME, contentValues, "id =?", new String[] {id});

Sqlite data don't show after close the apps

This is my MainActivity.
public class MainActivity extends Activity {
EditText etName, etEmail;
DatabaseHelper dbHelper;
Button save;
// declare view
ListView lvEmployees;
// declare adapter
CustomizedAdapter adapter;
// datasource
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etEmail = (EditText) findViewById(R.id.etEmail);
save = (Button) findViewById(R.id.btnSave);
lvEmployees = (ListView) findViewById(R.id.lvEmployees);
dbHelper = new DatabaseHelper(this);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
save(v);
}
});
}
public void save(View v) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
Employee employee = new Employee(name, email);
Toast.makeText(getApplicationContext(), employee.toString(),
Toast.LENGTH_LONG).show();
long inserted = dbHelper.insertEmployee(employee);
if (inserted >= 0) {
Toast.makeText(getApplicationContext(), "Data inserted",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Data insertion failed...",
Toast.LENGTH_LONG).show();
}
ArrayList<Employee> employees = dbHelper.getAllEmployees();
if (employees != null && employees.size() > 0) {
adapter = new CustomizedAdapter(this, employees);
lvEmployees.setAdapter(adapter);
}
}
}
This is my DataBaseHelper.
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "task_management";
public static final int DB_VERSION = 1;
public static final String EMPLOYEE_TABLE = "employee";
public static final String ID_FIELD = "_id";
public static final String NAME_FIELD = "name";
public static final String EMAIL_FIELD = "email";
public static final String EMPLOYEE_TABLE_SQL = "CREATE TABLE "
+ EMPLOYEE_TABLE + " (" + ID_FIELD + " INTEGER PRIMARY KEY, "
+ NAME_FIELD + " TEXT, " + EMAIL_FIELD + " DATETIME);";
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
// create tables
db.execSQL(EMPLOYEE_TABLE_SQL);
Log.e("TABLE CREATE", EMPLOYEE_TABLE_SQL);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// upgrade logic
}
// insert
public long insertEmployee(Employee emp) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(NAME_FIELD, emp.getName());
values.put(EMAIL_FIELD, emp.getEmail());
long inserted = db.insert(EMPLOYEE_TABLE, null, values);
db.close();
return inserted;
}
// query
public ArrayList<Employee> getAllEmployees() {
ArrayList<Employee> allEmployees = new ArrayList<Employee>();
SQLiteDatabase db = this.getReadableDatabase();
// String[] columns={NAME_FIELD, EMAIL_FIELD, PHONE_FIELD};
// SELECT * FROM EMPLOYEE;
Cursor cursor = db.query(EMPLOYEE_TABLE, null, null, null, null, null,
null);
// Cursor cursor = db.rawQuery("SELECT * FROM EMPLOYEE", null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
//
int id = cursor.getInt(cursor.getColumnIndex(ID_FIELD));
String name = cursor.getString(cursor
.getColumnIndex(NAME_FIELD));
String email = cursor.getString(cursor
.getColumnIndex(EMAIL_FIELD));
Employee e = new Employee(id, name, email);
allEmployees.add(e);
cursor.moveToNext();
}
}
cursor.close();
db.close();
return allEmployees;
}
}
When i put data and pressed the save button then my data is saved and show in my ListView.
But when i close the apps and open it then i don't see any data in my ListView.
After putting data and pressed save button my new and existing data show in my ListView.
So how can i show my existing data in ListView after open my apps and without press the save button.
If you want to show data each time app starts, you would need to move your list populating code in onCreate
Move this code to onCreate instead of Save button's onClick
ArrayList<Employee> employees = dbHelper.getAllEmployees();
if (employees != null && employees.size() > 0) {
adapter = new CustomizedAdapter(this, employees);
lvEmployees.setAdapter(adapter);
}
Hope it helps.
P.S: If you need to repopulate list after Save button's click, make a separate function which contains this code. And call tha function in onCreate as well as in Save button's onClick
You are setting the adapter for the list view in your save method that is called only when you actually press the save button. Here's the part where you do it.
ArrayList<Employee> employees = dbHelper.getAllEmployees();
if (employees != null && employees.size() > 0) {
adapter = new CustomizedAdapter(this, employees);
lvEmployees.setAdapter(adapter);
}
Thats why there is no data in the listview when you open the app.
You should do this in your onCreate method, and in your save you should do something like this:
1. declare the the arraylist of employees along with listview;
ArrayList<Employee> employees;
in your oncreate call this code that you should remove from the save method
employees = dbHelper.getAllEmployees();
if (employees != null && employees.size() > 0) {
adapter = new CustomizedAdapter(this, employees);
lvEmployees.setAdapter(adapter);
}
in save method just add one more item to the list, and notify adapter that there has been a change in the data it's displaying.
employees.add(employee);
adapter.notifyDataSetChanged();

display primary key after inserting in database

I am developing an android app, and i want to display the primary key in the textview so that every-time I edit a textfield, I will be using the primary key to update.can anyone help me with this? below is the inserting of data in the sqlite. My problem is how to get the primary key...
public class UsedataActivity extends Activity {
DatabaseHandler db = new DatabaseHandler(this);
ImageButton evsave;
EditText evname;
EditText evtime;
EditText evdate;
EditText evcode;
TextView evadmin;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onetoone);
evsave = (ImageButton)findViewById(R.id.event_save);
evname = (EditText)findViewById(R.id.eventname);
evtime = (EditText)findViewById(R.id.time1);
evdate = (EditText)findViewById(R.id.eventdate);
evcode = (EditText)findViewById(R.id.eventcode);
evadmin = (TextView)findViewById(R.id.adminname_1to1);
evsave.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Events addev =
new Events(evname.getText().toString(),evcode.getText().toString(),evdate.getText().toString(),Integer.parseInt(evtime.getText().toString()),evadmin.getText().toString());
db.addEvents(addev);
Toast.makeText(getApplicationContext(), "Event: "+ evname.getText()+" successfully save",
Toast.LENGTH_SHORT).show();
}
});
}
database handler class:
public void addEvents(Events event) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EV_NAME, event.get_name());
values.put(KEY_EV_PASS, event.get_pass());
values.put(KEY_EV_DATE, event.get_date());
values.put(KEY_EV_TIME, event.get_time());
values.put(KEY_EV_ADMIN, event.get_admin());
// Inserting Row
db.insert(TABLE_EVENTS, null, values);
db.close();
}
As it can be observed from the docs for the SQLiteDatabase, db.insert will return the id of the newly created object. Just make addEvents return it (instead of being `void).
PS: Please paste code in edits of the question, not in comments. In comments they really look awful!
EDIT
public long addEvents(Events event) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EV_NAME, event.get_name());
values.put(KEY_EV_PASS, event.get_pass());
values.put(KEY_EV_DATE, event.get_date());
values.put(KEY_EV_TIME, event.get_time());
values.put(KEY_EV_ADMIN, event.get_admin());
// Inserting Row
long id = db.insert(TABLE_EVENTS, null, values);
db.close();
return id;
}
And then:
long id = db.addEvents(addev);
Toast.makeText(getApplicationContext(),
"Event with id: "+ id + " successfully saved",
Toast.LENGTH_SHORT).show();

SQL android, creating multiple tables more information

this is my code
`I have created one table, but i want to create two and when i hit the "show" button, i want to be able to select contents from both tables and show them...this is my code...am having problems creating two tables and showing them:
public class Entername extends Activity {
private Button showButton;
private Button insertButton;
private TextView nameEditText;
private TextView addTextView;
private Button doneButton;
public DatabaseHelper dbHelper = new DatabaseHelper(Entername.this,"pubgolfdatabase",2);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.entername);
addTextView = (TextView)findViewById(R.id.textView1);
doneButton= (Button)findViewById(R.id.doneButton);
insertButton = (Button)findViewById(R.id.addButton);
nameEditText = (EditText)findViewById(R.id.name);
showButton =(Button)findViewById(R.id.button1);
showButton.setOnClickListener(new showButtonListener());
insertButton.setOnClickListener(new InsertButtonListener());
doneButton.setOnClickListener(new DoneButtonListener());
/** create the database if it dosen't exist **/
SQLiteDatabase db = dbHelper.getWritableDatabase();
try
{
db.execSQL("create table user_name(ID integer, name varchar(90));");
}
catch(Exception e)
{
e.printStackTrace();
}
}
class InsertButtonListener implements OnClickListener, android.view.View.OnClickListener
{
public void onClick(View v)
{
if("".equals(nameEditText.getText().toString()))
{
Toast toast = Toast.makeText(Entername.this, "Sorry, you must input both the name and the address!", Toast.LENGTH_LONG);
toast.show();
}
else
{
long flag = 0;
int id = 1;
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.query("user_name", new String[]{"count(*) ID"}, null, null, null, null, null);
while(cursor.moveToNext())
{
int idFromDatabase = cursor.getInt(cursor.getColumnIndex("ID"));
if(idFromDatabase != 0)
{
id = 1 + idFromDatabase;
}
}
ContentValues values = new ContentValues();
values.put("ID", id);
values.put("name", nameEditText.getText().toString().trim());
flag = db.insert("user_name", null, values);
if(flag != -1)
{
Toast toast = Toast.makeText(Entername.this, "You have successful inserted this record into database! ", Toast.LENGTH_LONG);
toast.show();
db.close();
//clear fields //clearing edittexts
nameEditText.setText("");
return;
}
else
{
Toast toast = Toast.makeText(Entername.this, "An error occured when insert this record into database!", Toast.LENGTH_LONG);
toast.show();
db.close();
//clear fields
//clearing edittexts
nameEditText.setText("");
return;
}
}
}
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
}
}
class DoneButtonListener implements OnClickListener, android.view.View.OnClickListener
{
public void onClick(View v)
{
Intent myIntent = new Intent(v.getContext(), Pickholespubs.class);
startActivityForResult(myIntent, 0);
}
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
}
}
class showButtonListener implements OnClickListener, android.view.View.OnClickListener
{
public void onClick(View v)
{
String display = "";
SQLiteDatabase db = dbHelper.getWritableDatabase();
/** the result will be loaded in cursor **/
Cursor cursor = db.query("user_name", new String[]{"ID","name"}, null, null, null, null, null);
/** check if the table is empty **/
if (!cursor.moveToNext())
{
addTextView.setText("No data to display, please make sure you have already inserted data!");
db.close();
return;
}
cursor.moveToPrevious();
/** if the table is not empty, read the result into a string named display **/
while(cursor.moveToNext())
{
int ID = cursor.getInt(cursor.getColumnIndex("ID"));
String name = cursor.getString(cursor.getColumnIndex("name"));
display = display + "\n"+"Player"+ID+", Name: "+name;
}
/** display the result on the phone **/
addTextView.setText(display);
db.close();
}
public void onClick(DialogInterface dialog, int which)
{
// TODO Auto-generated method stub
}
}
}`
A Simple Answer would be No you can not do it. As Create Table Syntax doesn't allow two DML operations at a same time.
But the alternet way is like as follows,
Create Table table1 ( column list ); Create Table table2 ( column list );
This could be possible. Moral is there must be a ; (semicolon) after each Create Table syntax is completed).

Categories

Resources