parse.com查询到sqlite查询

时间:2016-05-03 04:06:42

标签: java android sqlite parse-platform

我正在尝试使用parse.com后端开发填字游戏到sqlite支持。我很难转换代码。 以下代码是我创建的数据库表。

<img src="imageURLHere.jpg">

我想在sqlite中使用以下代码:我试过但是它显示了一些错误,请帮助我!

public class puzzle extends SQLiteOpenHelper {

public static final String db_name = "crossword.db";
public static final String Table1_name = "puz";
public static final String Table2_name = "item";
public static final String Table3_name = "clue";

public puzzle(final Context context){
    super(context, db_name, null, 1);

}



@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS" + Table1_name + "hash INTEGER, title TEXT,  author TEXT,copyright TEXT,height INTEGER ,width INTEGER");
    db.execSQL("CREATE TABLE IF NOT EXISTS"+ Table2_name + "hash INTEGER, x INTEGER ,y INTEGER, cell position INTEGER , black BOOLEAN , letter TEXT");
    db.execSQL("CREATE TABLE IF NOT EXISTS" + Table3_name + "hash INTEGER,  x INTEGER ,y INTEGER, cellposition INTEGER, cluenumber INTEGER, position INTEGER,clue TEXT");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS" + Table1_name);
    db.execSQL("DROP TABLE IF EXISTS" + Table2_name);
    db.execSQL("DROP TABLE IF EXISTS" + Table3_name);
    onCreate(db);


}

public boolean insertitem(int hash, int x, int y, int cellposition, boolean black, String letter) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues itemvalues = new ContentValues();
    itemvalues.put("hash", hash);
    itemvalues.put("X", x);
    itemvalues.put("Y", y);
    itemvalues.put("CellPosition", cellposition);
    itemvalues.put("Black", black);
    itemvalues.put("Letter", letter);
    long itemresult = db.insert(Table2_name, null, itemvalues);
    if (itemresult == -1)
        return false;
    else
        return true;

}
public boolean isinsertpuz(int hash,String title,String author,String copyright,int height,int width)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues puzvalues = new ContentValues();
    puzvalues.put("hash",hash);
    puzvalues.put("Title",title);
    puzvalues.put("author",author);
    puzvalues.put("copyright",copyright);
    puzvalues.put("Height",height);
    puzvalues.put("Width",width);
    long puzresult = db.insert(Table1_name, null, puzvalues);
    if (puzresult == -1)
        return false;
    else
        return true;


}
public boolean isinsertclue(int hash,int x,int y,int cellposition,int cluenumber,String clue)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cluevalues = new ContentValues();
    cluevalues.put("hash",hash);
    cluevalues.put("X",x);
    cluevalues.put("Y",y);
    cluevalues.put("Cellposition",cellposition);
    cluevalues.put("Cluenumber",cluenumber);
    cluevalues.put("Clue",clue);
    long clueresult=db.insert(Table3_name,null,cluevalues);
    if(clueresult==-1)
        return false;
    else
        return true;
}
    public Cursor getpuzdata()
    {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor puzquery=db.rawQuery("select * from" +Table1_name,null);
        return puzquery;
    }}

}

以下代码是我尝试将代码从parse.com更改为sqlite

的方式
public CustomAdapter(Context context, int screenWidth, final int width,int screenHeight, int height, int hash, TextView clueTextView, TextView titleTextView){
    this.context = context;
    this.width = width;
    this.height = height;
    this.screenHeight = screenHeight;
    this.screenWidth = screenWidth;
    activity = (Activity)context;
    this.hash = hash;
    this.clueTextView = clueTextView;
    this.titleTextView = titleTextView;

    clueNumber = 0;

    //cell count
    count = width*height;

    //to store each cell view
    view = new View[count];

    //contains solution
    letterChar = new char[count];

    clueAcross = new HashMap<>();
    clueDown = new HashMap<>();

    Log.d("parseSize", String.valueOf(letterChar.length));

    //fetch data saved locally
    query = ParseQuery.getQuery("Item").fromLocalDatastore();
    query.whereEqualTo("hash", hash);
    query.orderByAscending("cellPosition");
            query.findInBackground(new FindCallback<ParseObject>() {
                @Override
                public void done(List<ParseObject> objects, ParseException e) {
                    if(e == null)
                        for (ParseObject parseObject : objects) {
                            if(parseObject.getString("letter").equals(""))
                                letterChar[parseObject.getInt("cellPosition")] = ' ';
                            else
                                letterChar[parseObject.getInt("cellPosition")] = parseObject.getString("letter").charAt(0);
                        }
                    ParseQuery.getQuery("Clue").fromLocalDatastore().whereEqualTo("hash", CustomAdapter.this.hash).orderByDescending("clueNumber").getFirstInBackground(new GetCallback<ParseObject>() {
                        @Override
                        public void done(ParseObject object, ParseException e) {
                            if(e == null)
                                clueSize = object.getInt("clueNumber");
                            Log.d("clueNumber", String.valueOf(clueSize));
                        }
                    });
                    ParseQuery.getQuery("Clue").fromLocalDatastore().whereEqualTo("hash", CustomAdapter.this.hash).orderByAscending("cellPosition").findInBackground(new FindCallback<ParseObject>() {
                        @Override
                        public void done(List<ParseObject> objects, ParseException e) {
                            if (e == null)
                                for (ParseObject object : objects) {
                                    //int pos = (object.getInt("y")*width)+object.getInt("x");
                                    if(object.getString("position").equals("Hor"))
                                        clueAcross.put(object.getInt("cellPosition"), object.getString("clue"));
                                    else
                                        clueDown.put(object.getInt("cellPosition"), object.getString("clue"));
                                    Log.d("clue", object.getInt("cellPosition")+" "+object.getString("clue")+" "+object.getString("position"));
                                }

                            //will populate the griview
                            Game.gridView.setAdapter(Game.customAdapter);

                            //to adjust layout on keyboard up
                            activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

                            //dismiss progress dialog on UI thread
                            activity.runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Game.progress.dismiss();
                                }
                            });
                        }
                    });
                }
            });

    //to fix clueNumber change on minimize and restore bug
    if(clueNumber > clueSize)
        clueNumber = 0;
        }

@Override
public int getCount() {
    return count;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    view[position] = convertView;
    if(view[position] == null){
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        view[position] = layoutInflater.inflate(R.layout.grid_cell, null);
        view[position].setLayoutParams(new RelativeLayout.LayoutParams(screenWidth / width, screenHeight / height));
    }

    //to shade black on cells containing no letters
    view[position].setBackgroundResource(R.drawable.shape);

    TextView cellNumberTextView = (TextView)view[position].findViewById(R.id.cellNumber);
    final EditText letter = (EditText)view[position].findViewById(R.id.letter);
    /*letter.setCursorVisible(false);
    letter.setClickable(false);
    letter.setLongClickable(false);
    letter.setFocusable(false);
    letter.setSelected(false);
    letter.setKeyListener(null);
    letter.setBackgroundResource(android.R.color.transparent);*/

    //letter.setText(Character.toString(letterChar[position]));
   // cellNumberTextView.setTextSize(cellNumberTextView.getTextSize() / 1.5f);

    //set clue number on cells
    if(clueAcross.get(position) != null){
        cellNumberTextView.setText(String.valueOf(clueNumber));
        clueNumber++;
        //to fix clueNumber change on minimize and restore bug
        if(clueNumber > clueSize)
            clueNumber = 0;
        Log.d("clueNumber", position+" "+clueNumber+ " "+clueSize);
    }
    else if(clueDown.get(position) != null){
        cellNumberTextView.setText(String.valueOf(clueNumber));
        clueNumber++;

        //to fix clueNumber change on minimize and restore bug
        if(clueNumber > clueSize)
            clueNumber = 0;
        Log.d("clueNumber", position+" "+clueNumber+ " "+clueSize);
    }


    //to disable EditText operations on black cells
    if(letterChar[position] == ' ') {
        view[position].setBackgroundColor(Color.BLACK);
        letter.setClickable(false);
        letter.setLongClickable(false);
        letter.setFocusable(false);
        letter.setSelected(false);
        letter.setKeyListener(null);
    }else
    view[position].setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //update clues on view click
            clueTextView.setText(getClueDown(position));
            titleTextView.setText(getClueAcross(position));

            //to open keyboard and focus on the letter to edit
            letter.requestFocus();
            letter.setCursorVisible(true);
            ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(letter, InputMethodManager.SHOW_IMPLICIT);
        }
    });


    //update clues on letter click
    letter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                letter.setText("");
                clueTextView.setText(getClueDown(position));
                titleTextView.setText(getClueAcross(position));
            }
        }
    });


    //check value and change color of cell accordingly
    letter.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //letter.setText(s.toString().toUpperCase());
            if(s.toString().isEmpty())
                return;
            Log.d("letter", "input "+s.toString()+"position "+String.valueOf(position)+Character.toString(letterChar[position]));
            if (s.toString().equals(Character.toString(letterChar[position]))) {
                view[position].setBackgroundResource(R.drawable.correct);
                focusNext(position);
            }
            else
                view[position].setBackgroundResource(R.drawable.wrong);
        }

        @Override
        public void afterTextChanged(Editable s) {
               /* if(s.toString().isEmpty())
                    return;*/
            //view[0].setBackgroundResource(R.drawable.correct);
        }
    });

    return view[position];
}

//to focus next cell
public void focusNext(int position) {
    for (int i = position; i < count; i++) {
        if (letterChar[i+1] == ' ')
            continue;
        else {
            EditText editText = (EditText) ((ViewGroup) view[i + 1]).getChildAt(0);
            editText.requestFocus();
            ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
            return;
        }
    }
}

//get ACROSS clue based on position
public String getClueAcross(int position){
    for(int i=position; i>=0; i--)
        if(clueAcross.get(i) != null)
            return "(across): "+clueAcross.get(i);
    return "";
}

//get DOWN clue based on position
public String getClueDown(int position){
    for(int i=position; i>=0; i-=width)
        if(clueDown.get(i) != null)
            return "(down): "+clueDown.get(i);

    return "";
}

@Override
public Object getItem(int position) {
    return view[position];
}

@Override
public long getItemId(int position) {
    return position;
}

} 这是我从parse转换为sqlite的代码,但是当我构建项目时,它仍保持不变。

1 个答案:

答案 0 :(得分:1)

我不确定这是否会对您有所帮助,但您的代码中存在SQL语法错误:

    db.execSQL("CREATE TABLE IF NOT EXISTS " + Table1_name + " (hash INTEGER, title TEXT,  author TEXT,copyright TEXT,height INTEGER ,width INTEGER)");
    db.execSQL("CREATE TABLE IF NOT EXISTS "+ Table2_name + " (hash INTEGER, x INTEGER ,y INTEGER, cell position INTEGER , black BOOLEAN , letter TEXT)");
    db.execSQL("CREATE TABLE IF NOT EXISTS " + Table3_name + " (hash INTEGER,  x INTEGER ,y INTEGER, cellposition INTEGER, cluenumber INTEGER, position INTEGER,clue TEXT)");

您在"("之前错过了"hash",在每个陈述结束时"EXISTS"")"之后的空格