对于MysqlPDO与正确的用户、pass&DB名称连接,但在工作中它显示SQLSTATE[42S02]:Basetableorviewnotfound:1146Table'database.tabl
对于Mysql PDO 与正确的用户、pass & DB 名称连接,但在工作中它显示 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.table'感兴趣的读者,本文将会是一篇不错的选择,并为您提供关于Android getReadableDatabase() 和 getWritableDatabase()分析对比、android – getWritableDatabase()和getReadableDatabase()之间的区别?、android – SQLiteDatabase getWritableDatabase()无法正常工作、android – SQLiteDatabase.openDatabase vs SQLiteOpenHelper.getReadableDatabase的有用信息。
本文目录一览:- Mysql PDO 与正确的用户、pass & DB 名称连接,但在工作中它显示 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.table'
- Android getReadableDatabase() 和 getWritableDatabase()分析对比
- android – getWritableDatabase()和getReadableDatabase()之间的区别?
- android – SQLiteDatabase getWritableDatabase()无法正常工作
- android – SQLiteDatabase.openDatabase vs SQLiteOpenHelper.getReadableDatabase
Mysql PDO 与正确的用户、pass & DB 名称连接,但在工作中它显示 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.table'
如何解决Mysql PDO 与正确的用户、pass & DB 名称连接,但在工作中它显示 SQLSTATE[42S02]: Base table or view not found: 1146 Table ''database.table''?
这是我的 PDO config.PHP 文件:
<?PHP
Class Database{
// private $server = "MysqL:host=localhost;dbname=bdonesho_ther";private $username = ''**********'';private $password = "*************";
private $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
protected $conn;
public function open(){
try{
$this->conn = new PDO($this->server,$this->username,$this->password,$this->options);
return $this->conn;
}
catch (PDOException $e){
echo "There is some problem in connection: " . $e->getMessage();
}
}
public function close(){
$this->conn = null;
}
}
$pdo = new Database();
?>
我的 sql 语法是:
try{
$stmt = $conn->prepare("SELECT * FROM category ORDER BY RAND() LIMIT 25");
$stmt->execute();
foreach($stmt as $row){}
}catch (PDOException $e){echo $e->getMessage();}
?>
显示:
sqlSTATE[42S02]:未找到基表或视图:1146 表 ''bdonesho_ther.products'' 不存在
我的数据库排序规则是 utf8_unicode_ci
我的表在 utf8_unicode_ci
,utf8_general_ci
,latin1_swedish_ci
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Android getReadableDatabase() 和 getWritableDatabase()分析对比
Android getReadableDatabase() 和 getWritableDatabase()分析对比
Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的sqliteDatabase实例。(getReadableDatabase()方法中会调用getWritableDatabase()方法)
其中getWritableDatabase() 方法以读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写,倘若使用的是getWritableDatabase() 方法就会出错。
getReadableDatabase()方法则是先以读写方式打开数据库,如果数据库的磁盘空间满了,就会打开失败,当打开失败后会继续尝试以只读方式打开数据库。如果该问题成功解决,则只读数据库对象就会关闭,然后返回一个可读写的数据库对象。
源码如下:
/** * Create and/or open a database that will be used for reading and writing. * Once opened successfully,the database is cached,so you can call this * method every time you need to write to the database. Make sure to call * {@link #close} when you no longer need it. * * <p>Errors such as bad permissions or a full disk may cause this operation * to fail,but future attempts may succeed if the problem is fixed.</p> * * @throws sqliteException if the database cannot be opened for writing * @return a read/write database object valid until {@link #close} is called */ public synchronized sqliteDatabase getWritableDatabase() { if (mDatabase != null && mDatabase.isopen() && !mDatabase.isReadOnly()) { return mDatabase; // The database is already open for business } if (mIsInitializing) { throw new IllegalStateException("getWritableDatabase called recursively"); } // If we have a read-only database open,someone Could be using it // (though they shouldn't),which would cause a lock to be held on // the file,and our attempts to open the database read-write would // fail waiting for the file lock. To prevent that,we acquire the // lock on the read-only database,which shuts out other users. boolean success = false; sqliteDatabase db = null; if (mDatabase != null) mDatabase.lock(); try { mIsInitializing = true; if (mName == null) { db = sqliteDatabase.create(null); } else { db = mContext.openorCreateDatabase(mName,mFactory); } int version = db.getVersion(); if (version != mNewVersion) { db.beginTransaction(); try { if (version == 0) { onCreate(db); } else { onUpgrade(db,version,mNewVersion); } db.setVersion(mNewVersion); db.setTransactionSuccessful(); } finally { db.endTransaction(); } } onopen(db); success = true; return db; } finally { mIsInitializing = false; if (success) { if (mDatabase != null) { try { mDatabase.close(); } catch (Exception e) { } mDatabase.unlock(); } mDatabase = db; } else { if (mDatabase != null) mDatabase.unlock(); if (db != null) db.close(); } } } /** * Create and/or open a database. This will be the same object returned by * {@link #getWritableDatabase} unless some problem,such as a full disk,* requires the database to be opened read-only. In that case,a read-only * database object will be returned. If the problem is fixed,a future call * to {@link #getWritableDatabase} may succeed,in which case the read-only * database object will be closed and the read/write object will be returned * in the future. * * @throws sqliteException if the database cannot be opened * @return a database object valid until {@link #getWritableDatabase} * or {@link #close} is called. */ public synchronized sqliteDatabase getReadableDatabase() { if (mDatabase != null && mDatabase.isopen()) { return mDatabase; // The database is already open for business } if (mIsInitializing) { throw new IllegalStateException("getReadableDatabase called recursively"); } try { return getWritableDatabase(); } catch (sqliteException e) { if (mName == null) throw e; // Can't open a temp database read-only! Log.e(TAG,"Couldn't open " + mName + " for writing (will try read-only):",e); } sqliteDatabase db = null; try { mIsInitializing = true; String path = mContext.getDatabasePath(mName).getPath(); db = sqliteDatabase.openDatabase(path,mFactory,sqliteDatabase.OPEN_READONLY); if (db.getVersion() != mNewVersion) { throw new sqliteException("Can't upgrade read-only database from version " + db.getVersion() + " to " + mNewVersion + ": " + path); } onopen(db); Log.w(TAG,"Opened " + mName + " in read-only mode"); mDatabase = db; return mDatabase; } finally { mIsInitializing = false; if (db != null && db != mDatabase) db.close(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
android – getWritableDatabase()和getReadableDatabase()之间的区别?
解决方法
公共同步sqliteDatabase getReadableDatabase()
自:API级别1
Create and/or open a database. This will be the same object returned
by getWritableDatabase() unless some problem,such as a full disk,
requires the database to be opened read-only. In that case,a
read-only database object will be returned. If the problem is fixed,a
future call to getWritableDatabase() may succeed,in which case the
read-only database object will be closed and the read/write object
will be returned in the future.
与getWritableDatabase()一样,此方法可能需要很长时间才能返回,因此您不应从应用程序主线程(包括ContentProvider.onCreate())调用它.
返回
a database object valid until getWritableDatabase() or close() is called.
抛出
sqliteException如果无法打开数据库
公共同步sqliteDatabase getWritableDatabase()
自:API级别1
Create and/or open a database that will be used for reading and
writing. The first time this is called,the database will be opened
and onCreate(sqliteDatabase),onUpgrade(sqliteDatabase,int,int)
and/or onopen(sqliteDatabase) will be called.Once opened successfully,the database is cached,so you can call this
method every time you need to write to the database. (Make sure to
call close() when you no longer need the database.) Errors such as bad
permissions or a full disk may cause this method to fail,but future
attempts may succeed if the problem is fixed.
数据库升级可能需要很长时间,您不应该从应用程序主线程调用此方法,包括从ContentProvider.onCreate().
返回
a read/write database object valid until close() is called
抛出sqliteException如果无法打开数据库进行写入
android – SQLiteDatabase getWritableDatabase()无法正常工作
02-15 08:36:13.097: I/sqliteDatabaseCpp(404): sqlite returned: error
code = 1,msg = near “null”: Syntax error,
db=/data/data/com.lifeApp/databases/contactsManager
在我的代码的第157行.这是该方法下的部分
getAllContacts()
无效的代码是
sqliteDatabase db = this.getWritableDatabase();
完整示例:
package com.lifeApp; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import java.util.ArrayList; import java.util.List; import android.content.ContentValues; import android.database.Cursor; public class DatabaseHandler extends sqliteOpenHelper { // All Static variables // Database Version private static final int DATABASE_VERSION = 1; // Database Name private static final String DATABASE_NAME = "contactsManager"; // Contacts table name private static final String TABLE_CONTACTS = "contacts"; // Contacts Table Columns names private static final String KEY_ID = "id"; private static final String FirsTNAME = "firstname"; private static final String LASTNAME = "lastname"; private static final String PASSWORD = "password"; private static final String EMAIL = "email"; private static final String KEY_PH_NO = "phone_number"; private static final String KEY_NAME = null; public DatabaseHandler(Context context) { super(context,DATABASE_NAME,null,DATABASE_VERSION); } // Creating Tables @Override public void onCreate(sqliteDatabase db) { String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + FirsTNAME + " TEXT," + LASTNAME + " TEXT," + PASSWORD + " TEXT" + EMAIL + " TEXT" + KEY_PH_NO + " TEXT" + ")"; db.execsql(CREATE_CONTACTS_TABLE); } // Upgrading database @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { // Drop older table if existed db.execsql("DROP TABLE IF EXISTS " + TABLE_CONTACTS); // Create tables again onCreate(db); } /** * All CRUD(Create,Read,Update,Delete) Operations */ // Adding new contact void addContact(Contact contact) { sqliteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME,contact.getName()); // Contact Name values.put(FirsTNAME,contact.getFirstname()); values.put(LASTNAME,contact.getLastname()); values.put(EMAIL,contact.getEmail()); values.put(PASSWORD,contact.getpassword()); values.put(KEY_PH_NO,contact.getPhoneNumber()); // Contact Phone // Inserting Row db.insert(TABLE_CONTACTS,values); db.close(); // Closing database connection } // Getting single contact Contact getContact(int id) { sqliteDatabase db = this.getReadableDatabase(); Cursor cursor = db.query(TABLE_CONTACTS,new String[] { KEY_ID,KEY_NAME,KEY_PH_NO },KEY_ID + "=?",new String[] { String.valueOf(id) },null); if (cursor != null) cursor.movetoFirst(); Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5),cursor.getString(6)); // return contact return contact; } // Getting All Contacts public List<Contact> getAllContacts() { List<Contact> contactList = new ArrayList<Contact>(); // Select All Query String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; sqliteDatabase db = this.getWritableDatabase(); Cursor cursor = db.rawQuery(selectQuery,null); // looping through all rows and adding to list if (cursor.movetoFirst()) { do { Contact contact = new Contact(); contact.setID(Integer.parseInt(cursor.getString(0))); contact.setFirstname(cursor.getString(1)); contact.setLastname(cursor.getString(2)); contact.setPassword(cursor.getString(3)); contact.setEmail(cursor.getString(4)); // Adding contact to list contactList.add(contact); } while (cursor.movetoNext()); } // return contact list return contactList; } // Updating single contact public int updateContact(Contact contact) { sqliteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_NAME,contact.getName()); values.put(FirsTNAME,contact.getLastname()); values.put(PASSWORD,contact.getpassword()); values.put(EMAIL,contact.getEmail()); values.put(KEY_PH_NO,contact.getPhoneNumber()); // updating row return db.update(TABLE_CONTACTS,values,KEY_ID + " = ?",new String[] { String.valueOf(contact.getID()) }); } // Deleting single contact public void deleteContact(Contact contact) { sqliteDatabase db = this.getWritableDatabase(); db.delete(TABLE_CONTACTS,new String[] { String.valueOf(contact.getID()) }); db.close(); } // Getting contacts Count public int getContactsCount() { String countQuery = "SELECT * FROM " + TABLE_CONTACTS; sqliteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery,null); cursor.close(); // return count return cursor.getCount(); } }
解决方法
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + FirsTNAME + " TEXT," + LASTNAME + " TEXT," + PASSWORD + " TEXT" <== + EMAIL + " TEXT" <== + KEY_PH_NO + " TEXT" + ")"; db.execsql(CREATE_CONTACTS_TABLE);
android – SQLiteDatabase.openDatabase vs SQLiteOpenHelper.getReadableDatabase
我应该使用哪种方法?基于我见过的示例代码,我首先使用sqliteOpenHelper创建数据库,但在需要使用数据库时调用sqliteDatabase.openDatabase.
解决方法
我们今天的关于Mysql PDO 与正确的用户、pass & DB 名称连接,但在工作中它显示 SQLSTATE[42S02]: Base table or view not found: 1146 Table 'database.table'的分享就到这里,谢谢您的阅读,如果想了解更多关于Android getReadableDatabase() 和 getWritableDatabase()分析对比、android – getWritableDatabase()和getReadableDatabase()之间的区别?、android – SQLiteDatabase getWritableDatabase()无法正常工作、android – SQLiteDatabase.openDatabase vs SQLiteOpenHelper.getReadableDatabase的相关信息,可以在本站进行搜索。
本文标签: