SQLiteException:找不到这样的表而是创建了一个表

时间:2017-10-19 17:53:30

标签: android mysql sqlite listview android-database

在此应用程序中,我有两个activities用于发送用户location,另一个用于显示listView,其中每行包含姓名和电话号码。 listView条目存储在sqlitedatabse中。在输入姓名和电话号码时,activity表示listView显示没有错误,但activity表示location按钮将sqlitedatabse发送到UserDataB中存储的号码1}}显示错误

  

android.database.sqlite.SQLiteException:没有这样的表:UserDataB(代码1):,同时编译:SELECT * FROM UserDataB

其中table namepublic 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); } } 。我检查了这个link,因为错误与我的错误类似,但它没有帮助我解决问题。代码如下:

UserDataBase

activity

location的{​​{1}}代码sqlitedatabse发送到 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(); } } 中的条目,如下所示:

活动

import { PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

constructor(@Inject(PLATFORM_ID) private platformId: Object) { ... }

ngOnInit() {
  if (isPlatformBrowser(this.platformId)) {
     // Client only code.
     ...
  }
} 

1 个答案:

答案 0 :(得分:1)

您正在使用两个不同的数据库文件User_DetailsUser_Details.DB

如果您使用SQLiteOpenHelper来管理数据库,请始终如一地使用它。例如,替换

db=Gps4Activity.this.openOrCreateDatabase("User_Details",MODE_PRIVATE,null);

类似

db = new UserDatabase(this).getReadableDatabase();

实际使用帮助程序为您创建数据库和表,并删除帮助程序中不存在的CREATE TABLE代码。