我已成功使用sqlite查询一段时间了。但是,当我将这些代码迁移到另一个APP的片段时。代码将在查询后立即崩溃。当进入这些代码时,变量似乎没问题。我的猜测是“上下文”没有给出正确的值,但是,我找不到它。以下是代码,请帮我找到问题。
问题解决:忘记将文件从资产复制到数据库目录。
Fragment.java
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Fragment1 extends Fragment {
private int position;
private ListView lv;
private static dbOperator dbOp;
private Cursor cursor;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context=getActivity();
View rootView = inflater.inflate(R.layout.fragment_list, container, false);
lv=(ListView) rootView.findViewById(R.id.list);
dbOp = new dbOperator(context);
dbOp.open();
fillListChatrooms();
return rootView;
}
@Override
public void onDestroyView(){
dbOp.close();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
private void fillListChatrooms() {
int visibility;
cursor = dbOp.readChatrooms();
ListAdapterChatrooms adapter = new ListAdapterChatrooms(context, cursor, dbOp);
lv.setAdapter(adapter);
}
}
dbOperator.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import java.io.IOException;
import java.util.Locale;
public class dbOperator {
private static final String dbName_friends = "friends.db";
private static final int dbVersion= 1;
public static final String tableCategories = "categories";
public static final String columnIDC = "id_c";
public static final String tableInterests = "interests";
public static final String columnIDI = "id_i";
public static final String columnIDU = "id_u";
public static final String columnName = "name";
public static final String columnLastChatTimestamp = "last_chat_timestamp";
public static final String columnMatchedPercentage = "matched_percentage";
public static final String columnIcon = "icon";
public static final String columnChatLog = "chat_log";
public static final String columnIDD = "id_d";
public static final String columnTimestamp = "timestamp";
public static final String columnContentFile = "content_file";
public static final String tableChatrooms = "chatrooms";
public static final String columnIDCR = "id_cr";
public static final String columnChatType = "type";
public static final String columnUnreadMessages = "unread_messages";
private String language;
private final Context context;
private dbOpenHelper dbHelper_friends;
private SQLiteDatabase db_friends;
private final String sdPath;
private String dbPath_friends ;
private static boolean flagDbModified_friends=false;
dbOperator(Context ctx){
context=ctx;
dbPath_friends = ctx.getDatabasePath(dbName_friends).getPath();
sdPath= Environment.getExternalStorageDirectory().getPath();
Locale current= ctx.getResources().getConfiguration().locale;
language=current.getCountry();
switch (language)
{
case "de":
break;
case "zh-rTW":
language="tw";
break;
default:
language="en";
break;
}
}
public String getLanguage()
{
return language;
}
public void open() throws SQLException {
boolean flagDBexist;
flagDBexist=fileOp.checkFileExist(dbPath_friends);
if (!flagDBexist ) copyDBfromServer(dbName_friends);
dbHelper_friends= new dbOpenHelper(context, dbName_friends );
db_friends=dbHelper_friends.getWritableDatabase();
}
public void close() {
dbHelper_friends.close();
}
private void copyDBfromServer(String dbName)
{
}
private void backupDB(String source, String target) throws IOException{
fileOp.copyFile(source, target);
}
public Cursor readChatrooms(){
Cursor c;
// Below is where the crash start
c=db_friends.query(tableChatrooms,null,null,null,null,null,columnChatType);
return c;
}
public class dbOpenHelper extends SQLiteOpenHelper {
public dbOpenHelper(Context context, String dbName) {
super(context, dbName, null, dbVersion);
// super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase sqldb) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
}
}
答案 0 :(得分:0)
将其称为片段
的onActivityCreated()方法@staff_member_required
def save_pen(request):
if request.method == 'POST':
values = request.META.items()
label = request.POST.get("label", "")
color = request.POST.get("color", "")
material = request.POST.get("material", "")
if len(label) > 0 and len(color) > 0 and len(material) > 0 and\
not color.startswith('-') and not material.startswith('-'):
import pdb;pdb.set_trace
col_obj = Color.objects.filter(color=color)[0]
mat_obj = Material.objects.filter(type=material, color=col_obj)[0]
pen_obj = Pen(label=label, material=mat_obj)
pen_obj.save()
return HttpResponseRedirect('/admin/penshop/pen/')
else:
raise Exception('Bad data. It is not going to be saved!')
else:
return HttpResponseRedirect('/admin/')
return render(request, 'index.html', {'form': form})