我已经创建了一个数据库和一个表。现在,我想在数据库中添加另一个表,并在其中添加新表。
df = pd.DataFrame({'col2': [list('abc'), list('de'), list('fghi')]})
flat_col2 = [element for row in df.col2 for element in row]
# ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
另一个是:-
package com.example.user.shopkeeper.sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.example.user.shopkeeper.model.User;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "manager.db";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PASSWORD = "user_password";
private static final String COLUMN_USER_MOBILE_NUMBER = "user_mobile_number";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_USER_NAME + " TEXT, "
+ COLUMN_USER_EMAIL + " TEXT, " + COLUMN_USER_PASSWORD + " TEXT, "+ COLUMN_USER_MOBILE_NUMBER + " TEXT" + ")";
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL(DROP_USER_TABLE);
onCreate(db);
}
public void addUser(User user){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME,user.getName());
values.put(COLUMN_USER_EMAIL,user.getEmail());
values.put(COLUMN_USER_PASSWORD,user.getPassword());
values.put(COLUMN_USER_MOBILE_NUMBER,user.getNumber());
db.insert(TABLE_USER,null,values);
db.close();
}
public boolean checkUser(String email, String password){
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getWritableDatabase();
String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ? ";
String[] selectionArgs = {email , password};
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null );
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if(cursorCount > 0){
return true;
}
else{
return false;
}
}
public boolean checkUser(String email){
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getWritableDatabase();
String selection = COLUMN_USER_EMAIL + " = ?" ;
String[] selectionArgs = {email};
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null );
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if(cursorCount > 0){
return true;
}
else{
return false;
}
}
public List<String> getContactNumber(String email){
List<String> a = new ArrayList<>();
String[] contact = new String[]{email};
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER +", " + COLUMN_USER_ID + ", " +COLUMN_USER_NAME + " FROM " + TABLE_USER + " WHERE " + COLUMN_USER_EMAIL + " =?";
Cursor c = db.rawQuery(query, contact);
if(c.moveToFirst()){
a.add(c.getString(c.getColumnIndex(COLUMN_USER_MOBILE_NUMBER)));
a.add(c.getString(c.getColumnIndex(COLUMN_USER_NAME)));
a.add(c.getString(c.getColumnIndex(COLUMN_USER_ID)));
}
db.close();
c.close();
return a;
}
}
我想知道如何在现有数据库中添加表。 它包括一些要修改的升级部分,我找不到它。我是Android新手,无法搜索正确的代码对其进行修改。
我得到的错误是找不到表客户端。
答案 0 :(得分:0)
未找到(将要获得)表的原因是数据库存储在磁盘上,即它是持久性的。它被创建一次。一旦存在,就不会重新创建。因此(不同于活动) onCreate 方法仅在数据库的生命周期内运行一次(除非另外调用,例如,您经常会从onUpgrade方法中看到它)。
因此,您必须相应地设计使用 onCreate 。
在您的情况下,最简单的方法可能是将两个类结合起来,例如
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2; //<<<<<<<<<< Changed so will call onUpgrade
private static final String DATABASE_NAME = "manager.db";
private static final String TABLE_USER = "user";
private static final String COLUMN_USER_ID = "user_id";
private static final String COLUMN_USER_NAME = "user_name";
private static final String COLUMN_USER_EMAIL = "user_email";
private static final String COLUMN_USER_PASSWORD = "user_password";
private static final String COLUMN_USER_MOBILE_NUMBER = "user_mobile_number";
private String CREATE_USER_TABLE = "CREATE TABLE " + TABLE_USER + "("
+ COLUMN_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_USER_NAME + " TEXT, "
+ COLUMN_USER_EMAIL + " TEXT, " + COLUMN_USER_PASSWORD + " TEXT, "+ COLUMN_USER_MOBILE_NUMBER + " TEXT" + ")";
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + TABLE_USER;
private final static String TABLE_CLIENT = "clients";
private final static String _ID = BaseColumns._ID;
//private final static String COLUMN_USER_ID = "user_id";
private final static String CLIENT_USER_ID = "client_user_id";
private final static String COLUMN_CLIENT_NAME = "name";
private final static String COLUMN_CLIENT_GENDER = "gender";
private final static String COLUMN_CLIENT_MOBILE_NUMBER = "mobile_number";
private final static String COLUMN_AMOUNT = "amount";
public final static int GENDER_UNKNOWN = 0;
public final static int GENDER_MALE = 1;
public final static int GENDER_FEMALE = 2;
private String CREATE_CLIENT_TABLE = "CREATE TABLE " + TABLE_CLIENT + "( "
+ CLIENT_USER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_USER_ID + " INTEGER, "
+ COLUMN_CLIENT_NAME + " TEXT, " + COLUMN_CLIENT_MOBILE_NUMBER + " TEXT, " + COLUMN_CLIENT_GENDER + " TEXT, "
+ COLUMN_AMOUNT + " INTEGER" + " )";
private String DROP_CLIENT_TABLE = "DROP TABLE IF EXISTS " + TABLE_CLIENT;
public DatabaseHelper(Context context){
super(context, DATABASE_NAME,null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER_TABLE);
db.execSQL(CREATE_CLIENT_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL(DROP_USER_TABLE);
db.execSQL(DROP_CLIENT_TABLE);
onCreate(db);
}
public void addUser(User user){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_NAME,user.getName());
values.put(COLUMN_USER_EMAIL,user.getEmail());
values.put(COLUMN_USER_PASSWORD,user.getPassword());
values.put(COLUMN_USER_MOBILE_NUMBER,user.getNumber());
db.insert(TABLE_USER,null,values);
db.close();
}
public boolean checkUser(String email, String password){
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getWritableDatabase();
String selection = COLUMN_USER_EMAIL + " = ?" + " AND " + COLUMN_USER_PASSWORD + " = ? ";
String[] selectionArgs = {email , password};
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null );
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if(cursorCount > 0){
return true;
}
else{
return false;
}
}
public boolean checkUser(String email){
String[] columns = {
COLUMN_USER_ID
};
SQLiteDatabase db = this.getWritableDatabase();
String selection = COLUMN_USER_EMAIL + " = ?" ;
String[] selectionArgs = {email};
Cursor cursor = db.query(TABLE_USER,
columns,
selection,
selectionArgs,
null,
null,
null );
int cursorCount = cursor.getCount();
cursor.close();
db.close();
if(cursorCount > 0){
return true;
}
else{
return false;
}
}
public List<String> getContactNumber(String email){
List<String> a = new ArrayList<>();
String[] contact = new String[]{email};
SQLiteDatabase db = this.getReadableDatabase();
String query = "SELECT " + COLUMN_USER_MOBILE_NUMBER +", " + COLUMN_USER_ID + ", " +COLUMN_USER_NAME + " FROM " + TABLE_USER + " WHERE " + COLUMN_USER_EMAIL + " =?";
Cursor c = db.rawQuery(query, contact);
if(c.moveToFirst()){
a.add(c.getString(c.getColumnIndex(COLUMN_USER_MOBILE_NUMBER)));
a.add(c.getString(c.getColumnIndex(COLUMN_USER_NAME)));
a.add(c.getString(c.getColumnIndex(COLUMN_USER_ID)));
}
db.close();
c.close();
return a;
}
public boolean checkUserMobile(String contact){
String[] columns = {
CLIENT_USER_ID
};
SQLiteDatabase db = this.getReadableDatabase();
String selection = COLUMN_CLIENT_MOBILE_NUMBER + " =? ";
String[] selectionArgs = {contact};
Cursor cursor = db.query(TABLE_CLIENT,
columns,
selection,
selectionArgs,
null,
null,
null );
int count = cursor.getCount();
cursor.close();
db.close();
if(count > 0){
return true;
} else{
return false;
}
}
public void addClient(User user){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USER_ID,user.getUserId());
values.put(COLUMN_CLIENT_NAME,user.getName());
values.put(COLUMN_CLIENT_MOBILE_NUMBER,user.getNumber());
values.put(COLUMN_CLIENT_GENDER,user.getGender());
values.put(COLUMN_AMOUNT,user.getAmount());
db.insert(TABLE_CLIENT,null,values);
db.close();
}
}
请注意注释/// <<<<<<<<<<已更改,因此将调用onUpgrade
您可能会注意到行//private final static String COLUMN_USER_ID = "user_id";
已被注释掉,这是因为在两个原始类中都定义了COLUMN_USER_ID
。