为什么我的SQL服务器不工作的权利(Android SDK中)? [英] Why is my SQL server not working right(android SDK)?

查看:75
本文介绍了为什么我的SQL服务器不工作的权利(Android SDK中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的记事本教程和我的SQL服务器是一个修改后的版本。出于某种原因,我的应用程序不会写它正确。任何帮助将大大AP preciated。我不明白的code部分是错误的。

 包com.drawing;

进口android.content.ContentValues​​;
进口android.content.Context;
进口android.database.Cursor;
进口android.database.SQLException;
进口android.database.sqlite.SQLiteDatabase;
进口android.database.sqlite.SQLiteOpenHelper;
进口android.util.Log;

公共类NotesDbAdapter {

 公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_X =X;
公共静态最后弦乐KEY_Y =Y;
公共静态最后弦乐KEY_Size =大小;
私有静态最后字符串变量=NotesDbAdapter;
私人DatabaseHelper mDbHelper;
私人SQLiteDatabase MDB;
私有静态最后弦乐DATABASE_NAME =数据;
私有静态最后弦乐DATABASE_TABLE =注意事项;
私有静态最终诠释DATABASE_VERSION = 2;
私有静态最后弦乐DATABASE_CREATE =
          创建表+ DATABASE_TABLE +(
         + KEY_ROWID +整数主键自动增量,
         + KEY_X +文字不为空,
         + KEY_Y +文字不为空,
         + KEY_Size +文字NOT NULL);;

    私人最终语境mCtx;

    私有静态类DatabaseHelper扩展SQLiteOpenHelper {

        DatabaseHelper(上下文的背景下){
            超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        }


            @覆盖
            公共无效的onCreate(SQLiteDatabase DB){
                尝试 {
                    db.execSQL(DATABASE_CREATE);
                }赶上(例外五){
                    Log.e(dbAdapter,e.getMessage()的toString());
                }
            }

        @覆盖
        公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
            Log.w(TAG,从版本升级数据库+ oldVersion +到
                    +动态网页+,这将销毁所有旧数据);
            db.execSQL(DROP TABLE IF EXISTS笔记);
            的onCreate(DB);
        }

    }

    公共NotesDbAdapter(上下文CTX){
        this.mCtx = CTX;
    }

    公共NotesDbAdapter的open()抛出的SQLException {
        mDbHelper =新DatabaseHelper(mCtx);
        MDB = mDbHelper.getWritableDatabase();
        回到这一点;
    }

    公共无效的close(){
        mDbHelper.close();
    }


    众长createNote(浮动楼浮克,INT尺寸){
        ContentValues​​ initialValues​​ =新ContentValues​​();
        initialValues​​.put(KEY_X,F);
        initialValues​​.put(KEY_Y,G);
        initialValues​​.put(KEY_Size,大小);
        返回mDb.insert(DATABASE_TABLE,空,initialValues​​);
    }

    公共布尔deleteNote(长ROWID){

        返回mDb.delete(DATABASE_TABLE,KEY_ROWID +=+ ROWID,NULL)> 0;
    }


    公共光标fetchAllNotes(){

        返回mDb.query(DATABASE_TABLE,新的String [] {KEY_ROWID,KEY_X,KEY_Y,KEY_Size},NULL,NULL,NULL,NULL,NULL);
    }

    公共光标fetchNote(长ROWID)抛出的SQLException {

        光标mCursor =

            mDb.query(真,DATABASE_TABLE,新的String [] {KEY_ROWID,KEY_X,KEY_Y,KEY_Size},KEY_ROWID +=+ ROWID,空,
                    NULL,NULL,NULL,NULL);
        如果(mCursor!= NULL){
            mCursor.moveToFirst();
        }
        返回mCursor;

    }

    公共布尔updateNote(长ROWID,诠释的x,INT Y,INT尺寸){
        ContentValues​​的args =新ContentValues​​();
        args.put(KEY_X中,x);
        args.put(KEY_Y,Y);
        args.put(KEY_Size,大小);
        返回mDb.update(DATABASE_TABLE,ARGS,KEY_ROWID +=+ ROWID,NULL)> 0;
    }
}
 

解决方案

我想在这里开始你的问题:

 公共静态最后弦乐KEY_ROWID =_id;
公共静态最后弦乐KEY_X =_id;
公共静态最后弦乐KEY_Y =_id;
公共静态最后弦乐KEY_Size =_id;
 

我想这应该是更像问题我链接:

 公共静态最后弦乐KEY_BOOK =书;
公共静态最后弦乐KEY_AUTHOR =作者;
公共静态最后弦乐KEY_ISBN =ISBN;
公共静态最后弦乐KEY_RATING =等级;
公共静态最后弦乐KEY_ROWID =_id;
 

二,你的表的创建也似乎是错误的:

 私有静态最后弦乐DATABASE_CREATE =
            创建表+ DATABASE_TABLE +(
                     + KEY_ROWID +整数主键自动增量,
                     + KEY_X +整数主键自动增量,
                     + KEY_Y +整数主键自动增量,
                     + KEY_Size +整数主键自动增量;
 

您已经创建了所有领域的主键,自动增量 - 这并没有真正意义。再看着另一个问题:

 私有静态最后弦乐DATABASE_CREATE =
      创建表+ DATABASE_TABLE +(
     + KEY_ROWID +整数主键自动增量,
     + KEY_AUTHOR +文字不为空,
     + KEY_BOOK +文字不为空,
     + KEY_ISBN +文字不为空,
     + KEY_RATING +文字NOT NULL);;
 

这更有道理。

请仔细看看下面的问题 - 重读你已经开始使用教程

我不断收到错误的SQLException这个

这里的问题可能有问题,但似乎可以正确识别列(这是你似乎有问题)。这些问题的答案通过一些其他步骤 - 我建议你看一看在相关建议,在这里右侧的你自己的问题。您可能会发现更多的三分球带领你在正确的方向。

I did the notepad tutorials and my SQL server is a modified version of that. For some reason my app is not writing to it correctly. Any help would be greatly appreciated. I dont understand what part of the code is wrong.

    package com.drawing;

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.util.Log;

public class NotesDbAdapter {

 public static final String KEY_ROWID = "_id";
public static final String KEY_X = "x";
public static final String KEY_Y = "y";
public static final String KEY_Size = "size";
private static final String TAG = "NotesDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "notes";
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE =
          " create table " +  DATABASE_TABLE  + " ("
         + KEY_ROWID + " integer primary key autoincrement,  "
         + KEY_X + " text not null, "
         + KEY_Y + " text not null, "
         + KEY_Size + " text not null);"; 

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }


            @Override
            public void onCreate(SQLiteDatabase db) {
                try {
                    db.execSQL(DATABASE_CREATE);  
                } catch (Exception e) {
                    Log.e("dbAdapter", e.getMessage().toString());
                }
            }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db); 
        }

    }

    public NotesDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    public NotesDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


    public long createNote(float f, float g, int size) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_X, f);
        initialValues.put(KEY_Y, g);
        initialValues.put(KEY_Size, size);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    public boolean deleteNote(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }


    public Cursor fetchAllNotes() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_X,KEY_Y,KEY_Size}, null, null, null, null, null);
    }

    public Cursor fetchNote(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_X,KEY_Y,KEY_Size}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    public boolean updateNote(long rowId, int x, int y, int size) {
        ContentValues args = new ContentValues();
        args.put(KEY_X, x);
        args.put(KEY_Y, y);
        args.put(KEY_Size, size);
        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

解决方案

I think your problem is initiated here:

public static final String KEY_ROWID = "_id";     
public static final String KEY_X = "_id";     
public static final String KEY_Y = "_id";     
public static final String KEY_Size = "_id"; 

I would guess it should be more like in the question I link to:

public static final String KEY_BOOK = "book"; 
public static final String KEY_AUTHOR = "author"; 
public static final String KEY_ISBN = "isbn"; 
public static final String KEY_RATING = "rating"; 
public static final String KEY_ROWID = "_id"; 

Second, your table creation also seems wrong:

private static final String DATABASE_CREATE = 
            " create table " +  DATABASE_TABLE  + " (" 
                     + KEY_ROWID + " integer primary key autoincrement,  "
                     + KEY_X + " integer primary key autoincrement, "
                     + KEY_Y + " integer primary key autoincrement, "
                     + KEY_Size + " integer primary key autoincrement, ";

You have created all fields as primary key and autoincrement - that doesn't really make sense. Again looking at the other question:

private static final String DATABASE_CREATE =
      " create table " +  DATABASE_TABLE  + " ("
     + KEY_ROWID + " integer primary key autoincrement,  "
     + KEY_AUTHOR + " text not null, "
     + KEY_BOOK + " text not null, "
     + KEY_ISBN + " text not null, "
     + KEY_RATING + " text not null);"; 

This makes much more sense.

Please take a closer look at the question below - and re-read the tutorials you have started with.

I keep getting the SQLException error with this

The question here may have problems, but seem to identify the columns properly (which is where you seem to have problems). The answers go through some other steps - and I recommend that you take a look at the 'Related' suggestions here on the right side of your own question. You may find more pointers to lead you in the right direction.

这篇关于为什么我的SQL服务器不工作的权利(Android SDK中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆