Reputation: 335
In this application I have two activities
one for sending location
of the user and other for displaying a listView
where each row contains a name and a phone number. The listView
entries are stored in a sqlitedatabse
. The activity
which conatins the listView
show no error when entering a name and a phone number but the activity
which conatins button to send location
to the numbers stored in the sqlitedatabse
shows an error
android.database.sqlite.SQLiteException: no such table: UserDataB (code 1): , while compiling: SELECT * FROM UserDataB
where UserDataB
is the table name
. I checked out this link because the error was similar to that of mine but it did not help me solving the issue. The code is given below :
UserDataBase
public class UserDatabase extends SQLiteOpenHelper {
// Table Name
public static final String TABLE_NAME = "UserDataB";
// Table columns
public static final String _ID = "_id";
public static final String NAME = "name";
public static final String NUMBER = "phonenumber";
// Database Information
static final String DB_NAME = "User_Details.DB";
// database version
static final int DB_VERSION = 1;
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT NOT NULL, " + NUMBER + " TEXT);";
public UserDatabase(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
The code for the activity
in which location
is sent to the entries in sqlitedatabse
is given below :
Activity
public class Gps4Activity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener {
private static final String LOG_TAG = "PlacesAPIActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private GoogleApiClient mGoogleApiClient;
private static final int PERMISSION_REQUEST_CODE = 100;
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ;
private TextView display;
private Button location_button,contacts_button;
//String number="xxxxxxxxxx";
ArrayList<String> numbers;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps4);
numbers=new ArrayList<>();
db=Gps4Activity.this.openOrCreateDatabase("User_Details",MODE_PRIVATE,null);
location_button=(Button)findViewById(R.id.show_button);
contacts_button=(Button)findViewById(R.id.view_button);
display=(TextView)findViewById(R.id.location_textview);
mGoogleApiClient = new GoogleApiClient.Builder(Gps4Activity.this)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
.build();
location_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mGoogleApiClient.isConnected()) {
if (ActivityCompat.checkSelfPermission(Gps4Activity.this,
android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Gps4Activity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSION_REQUEST_CODE);
ActivityCompat.requestPermissions(Gps4Activity.this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
callPlaceDetectionApi();
}
});
contacts_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(Gps4Activity.this,DetailsActivity.class);
startActivity(intent);
}
});
}
public ArrayList<String> getContacts(){
Cursor cursor=db.rawQuery("SELECT * FROM "+UserDatabase.TABLE_NAME,null);
db.execSQL("create table " + UserDatabase.TABLE_NAME + "(" + UserDatabase._ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + UserDatabase.NAME + " TEXT NOT NULL, " +
UserDatabase.NUMBER + " TEXT);");
while (cursor.moveToNext()){
String contact=cursor.getString(cursor.getColumnIndex(UserDatabase.NUMBER));
numbers.add(contact);
}
return numbers;
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e(LOG_TAG, "Google Places API connection failed with error code: "
+ connectionResult.getErrorCode());
Toast.makeText(this,
"Google Places API connection failed with error code:" +
connectionResult.getErrorCode(),
Toast.LENGTH_LONG).show();
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callPlaceDetectionApi();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
return;
}
break;
}
}
private void callPlaceDetectionApi() throws SecurityException {
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
.getCurrentPlace(mGoogleApiClient, null);
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
@Override
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
for (PlaceLikelihood placeLikelihood : likelyPlaces) {
Log.i(LOG_TAG, String.format("Place '%s' with " +
"likelihood: %g",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
display.setText(placeLikelihood.getPlace().getAddress().toString());
messageSending(placeLikelihood.getPlace().getAddress().toString());
break;
}
likelyPlaces.release();
}
});
}
public void messageSending(String message){
SmsManager smsManager = SmsManager.getDefault();
// smsManager.sendTextMessage(number, null, message, null, null);
getContacts();
smsManager.sendTextMessage(String.valueOf(numbers),null,message,null,null);
Toast.makeText(getApplicationContext(), "SMS sent."+String.valueOf(numbers),
Toast.LENGTH_LONG).show();
}
}
Upvotes: 0
Views: 455
Reputation: 152807
You are operating on two different database files, User_Details
and User_Details.DB
.
If you use SQLiteOpenHelper
for managing your database, use it consistently. For example, replace
db=Gps4Activity.this.openOrCreateDatabase("User_Details",MODE_PRIVATE,null);
with something like
db = new UserDatabase(this).getReadableDatabase();
to actually use the helper for creating the database and tables for you, and remove the CREATE TABLE
code that is not in the helper.
Upvotes: 1